实验代码:
tic
a = [];
for k = 1:10000
a(end+1, :) = rand(1, 100);
end
toc
tic
b = cell(0);
for k = 1:10000
b{end+1, 1} = rand(1, 100);
end
c = cell2mat(b);
toc
实验结果:
Elapsed time is 4.048442 seconds.
Elapsed time is 0.101062 seconds.
这表明,在matlab中,使用cell数组作为动态增长容器更高效。