Reshape cell array into a matrix

1 回表示 (過去 30 日間)
lightroman
lightroman 2017 年 11 月 17 日
編集済み: Stephen23 2017 年 11 月 17 日
I am struggling to reshape this cell array into a matrix of # cell arrays by max length of cell array. This ex 4x7 matrix. The real cell array is much much larger, so I didnt want to loop and build each row.
a{1} = {1 2 3 4 5}
a{2} = {2 2 4 5}
a{3} = {8 2}
a{4} = {8 2 9 9 2 1 3}
result = [1 2 3 4 5 0 0;
2 2 4 5 0 0 0;
8 2 0 0 0 0 0;
8 2 9 9 2 1 3]

採用された回答

Stephen23
Stephen23 2017 年 11 月 17 日
編集済み: Stephen23 2017 年 11 月 17 日
Assuming that those should be numeric vectors:
a{1} = [1 2 3 4 5];
a{2} = [2 2 4 5];
a{3} = [8 2];
a{4} = [8 2 9 9 2 1 3];
v = cellfun('size',a,2);
r = numel(a);
M = zeros(r,max(v));
for k = 1:r
M(k,1:v(k)) = a{k};
end
Giving:
>> M
M =
1 2 3 4 5 0 0
2 2 4 5 0 0 0
8 2 0 0 0 0 0
8 2 9 9 2 1 3
>>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by