Create a Matrix from different ccella array.

I have a cell array "data" 1x3 cell, each cell having different number of elements with 4 columns. for example data= [13252*4 double];[1516*4] double;[1244*4]double.
Now, I need a matrix which has maximum number of columns equal to 12 (sum of single columns) and rows equal to the maximum number of columns of all the cells. and I want to insert Nan when there is not value.
Thank you so much!!!

1 件のコメント

Fredic
Fredic 2020 年 7 月 24 日
I try this code
nCol = cellfun('size', data,1);
nRow = numel(data) ;
Output = NaN(max(nCol), nRow);
for iA = 1:numel(data)
idx = (iA - 1) + 1;
Output(1:nCol(iA), idx:idx+1) = data{iA};
end
But then appers this error
Unable to perform
assignment because the
size of the left side is
1352-by-2 and the size of
the right side is
1352-by-4.
Error in Fitting_function
(line 44)
Output(1:nCol(iA),
idx:idx+1) = data{iA};

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2020 年 7 月 24 日

0 投票

A{1} = rand(10,4) ;
A{2} = rand(5,4) ;
A{3} = rand(6,4) ;
% Get length of each cell
L = cellfun(@length,A) ;
for i = 1:length(L)
if size(A{i},1) < max(L)
T = NaN(max(L),4) ;
T(1:L(i),:) = A{i} ;
A{i} = T ;
end
end
iwant = [A{:}] ;

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 7 月 24 日
編集済み: madhan ravi 2020 年 7 月 24 日

0 投票

m = cellfun(@size, data, 'un', 0);
M = max(cat(1, m{:}));
Data = cellfun(@(x) [x; nan(M(1) - size(x, 1), M(2))], data, 'un', 0)

9 件のコメント

Fredic
Fredic 2020 年 7 月 24 日
Thank you so much, but in this way I have another cell array(Data). I want a matrix with lenght 1516 (that is the max value) and 12 columns....
madhan ravi
madhan ravi 2020 年 7 月 24 日
13252 Is the maximum value
Fredic
Fredic 2020 年 7 月 24 日
I have the same columns (number 4) and different lengths (1352,1516,1244)
Fredic
Fredic 2020 年 7 月 24 日
madhan ravi
madhan ravi 2020 年 7 月 24 日
編集済み: madhan ravi 2020 年 7 月 24 日
My answer gives you the desired result. You have some problems in copying.
Fredic
Fredic 2020 年 7 月 24 日
thanks!!
madhan ravi
madhan ravi 2020 年 7 月 24 日
You just wanted to use loop, ain’t it?
Fredic
Fredic 2020 年 7 月 24 日
no perfect
madhan ravi
madhan ravi 2020 年 7 月 24 日
Lol

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2020 年 7 月 24 日

コメント済み:

2020 年 7 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by