且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Matlab的单元格数组的每个单元格中插入数字?

更新时间:2023-01-15 19:24:06

您可以使用 CELLFUN :

b = cellfun(@(x)[10 x],a,'UniformOutput',0)


要回答@tmpearce评论,我使用了一个简单的脚本来衡量运行时间:


To answer @tmpearce comment I used a simple script to measure running time:

a = {[1 2 3]; [4 5]; [6 7 8 9]};
tic
a = cellfun(@(x)[10 x],a,'UniformOutput',0)
toc
a = {[1 2 3]; [4 5]; [6 7 8 9]}; 
tic
for ii=1:numel(a)
    a{ii} = [10 a{ii}];
end
toc

结果:

Elapsed time is 0.002622 seconds.
Elapsed time is 0.000034 seconds.