好吧,保持结构,这里有几种方法:
% Your original method
clear all
tic
population = 50;
individual = repmat(struct('genes', false(50,1), 'fitness', 0), population, 1);
toc
% simple loop
clear all
tic
population = 50;
individual(population,1) = struct('genes', false(50,1), 'fitness', 0);
for ii = 1:population
individual(ii).genes = false(50,1);
end
toc
% Third option
clear all
tic
population = 50;
individual = struct(...
'genes' , num2cell(false(50,population),2), ...
'fitness', num2cell(zeros(population,1)));
toc
结果:
Elapsed time is 0.009887 seconds. % your method
Elapsed time is 0.000475 seconds. % loop
Elapsed time is 0.013252 seconds. % init with cells
我的建议:只使用循环:)