Getting ith element of each cell array with different sizes in loop

1 回表示 (過去 30 日間)
NA
NA 2020 年 4 月 16 日
回答済み: Walter Roberson 2020 年 4 月 16 日
I have A
A ={[1;2;3;4],[5;6],[7;8;9;10],[17;13;15;20;25]};
val=cellfun(@(x) numel(x),A);
for i=1:length(1:max(val))
temp = cellfun(@(v)v(i),A)
%% use temp
end
In first loop
temp = [1,5,7,17]
In second loop
temp = [2,6,8,13]
In third loop I have error as Index exceeds the number of array elements (2).
How can I get this resul?
In third loop ---> temp = [3,6,9,15]
In forth loop ---> temp = [4,6,10,20]
In fifth loop ---> temp = [4,6,10,25]

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 16 日
temp = cellfun(@(v)v(min(end,i)),A)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by