Clear Filters
Clear Filters

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

7 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 9 Oct 2018
Edited: Jan on 9 Oct 2018
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

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!