2013-03-03 191 views

回答

6

您可以使用CELLFUN匿名函數:

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

要回答@tmpearce評論我用一個簡單的腳本來測量運行時間:

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. 
+3

這確實避免儘管可能值得檢查一下簡單循環是否有速度優勢 - cellfun/arrayfun往往非常慢 – tmpearce 2013-03-03 01:08:03

+0

對這樣一個小數據的比較幾乎沒有意義 – Amro 2013-04-22 10:01:40

相關問題