Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
|
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
for i=1:POPULATION_SIZE for j=1:NO_PARAMETERS c=dec2bin(parameters(j),NO_BITS_PARAMETER); chromosomes(i) = [chromosomes(i) c]; end end 上面的代码给出以下错误:
???类型为“ double”的输入参数的未定义函数或方法“ chromosomes”。我需要一个叫做chromosomes的空字符数组。我尝试在上述循环之前添加以下行。 chromosomes(1:POPULATION_SIZE)=''; 但它不起作用。它给出了错误 ??? Index of element to remove exceeds matrix dimensions. 回答: 您是否希望染色体是字符数组(当所有行的大小相同时)还是细胞数组(第ith个元素的大小可变)? 在第一种情况下,您将变量定义为: chromosomes = char(zeros(POPULATION_SIZE,NO_PARAMETERS*NO_BITS_PATAMETER)); 要么 chromosomes = repmat(' ',POPULATION_SIZE,NO_PARAMETERS*NO_BITS_PATAMETER); 然后在for循环中: chromosomes(i,(j-1)*NO_BITS_PATAMETER+1:j*NO_BITS_PATAMETER) = c; 对于单元格数组: chromosomes = cell(POPULATION_SIZE, NO_PARAMETERS); % each paramater in separate cell for i=1:POPULATION_SIZE for j=1:NO_PARAMETERS c=dec2bin(parameters(j),NO_BITS_PARAMETER); chromosomes{i,j} = c; end end 要么 chromosomes = cell(POPULATION_SIZE,1); % all parameters in a single cell per i for i=1:POPULATION_SIZE for j=1:NO_PARAMETERS c=dec2bin(parameters(j),NO_BITS_PARAMETER); chromosomes{i} = [chromosomes{i} c]; end end 编辑 : 实际上,您可以一次将DEC2BIN应用于整个数字数组。看起来每第ith行的变量parameters都相同。然后,您可以执行以下操作: c = dec2bin(parameters,NO_BITS_PARAMETER); chromosomes = reshape(c',1,[]); chromosomes = repmat(chromosomes,POPULATION_SIZE,1); 更多&回答... |
![]() |
![]() |