How to convert a cell array of different size to matrix of fixed size

4 次查看(过去 30 天)
I have a cell array A of dimension 1x22 each cell having different number of elements with compulsory 2 columns. for example A{1,1}= [ 23 34; 44 55; 56 71; 63 49; 71 30]
A{1,2}= [12 13; 10 99] and so on. Now, I need a matrix which has maximum number of columns equal to 44(twice the number of cell array columns) and rows equal to the maximum number of columns of all the cells. Here, it is 5(from A{1,1}) NaN or 0 values do not matter in my case.

采纳的回答

Jan
Jan 2018-10-9
编辑:Jan 2018-10-9
nCol = cellfun('size', A, 1);
nRow = numel(A) * 2;
Output = NaN(max(nCol), nRow);
But I guess you want to insert the values of the cell also. Then:
for iA = 1:numel(A)
idx = (iA - 1) * 2 + 1;
Output(1:nCol(iA), idx:idx+1) = A{iA};
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by