matlab - 遗传密码和僵尸!

标签 matlab

在外星世界,生物的遗传密码采用四进制(四进制)。 “13”和“22”对被认为是遗传性疾病。遗传密码长度为 n,如果至少有 n/4 个障碍,该生物就会变成僵尸!例如n=5,遗传密码01321的生物有障碍,但不是僵尸,而遗传密码22132的生物是僵尸(因为他有两种障碍,>n/4)。

现在我需要编写一个 MATLAB 程序,从用户那里获取一个值 n,这很简单,并显示生物的数量以及其中有多少僵尸

这是我到目前为止所写的内容,我不知道如何确定具有僵尸遗传密码的生物。我非常感谢您的想法和帮助。谢谢

n=input('Enter the length of the genetic sequence: ');
while (n<4) || (mod(n,1))~=0
disp('Invalid input!')
n=input('Enter the length of the genetic sequence: ');
end
nOfCreatures=4^n;
count=0;
for i=0:nOfCreatures
k=dec2base(i,4);
end
fprintf('There are %g creatures and %g of them are zombies.\n',nOfCreatures,count);

最佳答案

我在评论中推荐您尝试 REGEXP 功能。但实际上,如果您想计算重叠,例如将“222”计算为 2 个疾病,STRFIND 会更适合。

所以你需要这样的东西:

k=dec2base(i,4,n); %# use n to include trailing 0s, just for other possible types of disorders
m = [strfind(k,'13') strfind(k,'22')];
if numel(m) > n/4
    count = count+1;
end

此外,您可以将 n=0 作为第一行,而不是复制输入行。 并更正 for 循环以在 nOfCreatures-1 处结束。

编辑

矢量化解决方案的额外好处:

nOfCreatures=4^n;
k=cellstr(dec2base(0:nOfCreatures-1,4,n));
m = cellfun(@numel,strfind(k,'13')) + cellfun(@numel,strfind(k,'22'));
count = sum(m > n/4);
fprintf('There are %g creatures and %g of them are zombies.\n',nOfCreatures,count);

关于matlab - 遗传密码和僵尸!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5636540/

相关文章:

python - python中的指针

matlab - 是否可以在 MatLab 的脚本中定义局部函数?

matlab - 如何进行亲和/转换不同图像的图像配准

c++ - 特征:访问矩阵中的分散元素

matlab - 对 matlab 的结果进行切分或精确实数

matlab - 将 Matlab 结构打印到文本文件

database - 在 matlab 中创建多维 NetCDF

matlab - R 中 matlab 的 pcolor 的等价物是什么?

regex - 查找字符串中最短的重复模式

c++ - 我刚开始使用 Eigen 矩阵代数库,旨在创建数据集的相似矩阵,有什么建议吗?