How to convert cell to matrix
Mostra commenti meno recenti
I have a cell which is given by A= 1x13cell. I want to extract the contents of the cell into the form of a matrix of size (13 x number of points each cell containd) is this possible? The number of points in each cell varies. Can you help me with this?
2 Commenti
Jos (10584)
il 16 Lug 2013
I suggest you give a small example. For instance, if
C = {[1 2],[11 12 13]} % a cell array
how would you like the final matrix M to look like? Something like
1 2 NaN
11 12 13
?
Ede gerlderlands
il 16 Lug 2013
Risposte (3)
N = cellfun('length', C);
M = numel(C);
R = NaN(M, N);
for k = 1:M
R(1:N(k)) = C{k}; % Perhaps: reshape(C{k}, 1, [])
end
1 Commento
Ede gerlderlands
il 16 Lug 2013
Modificato: Ede gerlderlands
il 16 Lug 2013
David (degtusmc)
il 16 Lug 2013
Modificato: David (degtusmc)
il 16 Lug 2013
2 voti
Look up cell2mat. The link for the documentation is below. I hope this helps
3 Commenti
Ede gerlderlands
il 16 Lug 2013
Evan
il 16 Lug 2013
This will only work for cells arrays containing matrices of constant size in each array.
David (degtusmc)
il 18 Lug 2013
That is true. I did not think about that. Thank you Evan.
Jos (10584)
il 17 Lug 2013
Modificato: Jos (10584)
il 17 Lug 2013
Here is a suggestion
C = {[1 2 ;3 4],[11 12 13].',100:106} % a cell array
% make sure all elements are column vectors
C2 = cellfun(@(x) reshape(x,[],1), C,'un',0) ;
M = padcat(C2{:})
1 Commento
Tehmina Kakar
il 18 Lug 2018
Thank you so much @Jos. It works for me.
Categorie
Scopri di più su Multidimensional Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!