Concatenating cell arrays in matlab
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have three cell arrays:
a =
1×3 cell array
{37720×155 double} {37720×155 double} {37720×155 double}
b =
1×3 cell array
{13706×155 double} {13706×155 double} {13706×155 double}
c =
1×3 cell array
{10169×155 double} {10169×155 double} {10169×155 double}
I want to concatenate these vertically so I use:
for j = 1:3
z{j} = cat(1,a{j},b{j},c{j})
end
This does not work and I get the error: Cell contents assignment to a non-cell array object.
Please can someone help me combine these datasets so:
z =
1×3 cell array
{51426×155 double} {51426×155 double} {51426×155 double}
Many thanks,
Phil
0 commentaires
Réponses (1)
John D'Errico
le 22 Nov 2018
Modifié(e) : John D'Errico
le 22 Nov 2018
Easy peasy?
a
a =
1×3 cell array
{37720×155 double} {37720×155 double} {37720×155 double}
b
b =
1×3 cell array
{13706×155 double} {13706×155 double} {13706×155 double}
c
c =
1×3 cell array
{10169×155 double} {10169×155 double} {10169×155 double}
abc = cell(1,3);
for i = 1:3
abc{i} = [a{i};b{i};c{i}];
end
abc
abc =
1×3 cell array
{61595×155 double} {61595×155 double} {61595×155 double}
I think you were mistaken in the final expected size of z. As well, I think perhaps the array z already existed as an array of doubles, so when you tried to stuff elements into it as a cell array, you then got the error message that you did. Or, something like that.
Finally, I have a funny feeling that I can write the entire thing without a loop, but the result would not be terribly readable, and why bother? A simple loop is not a costly thing, and preoptimizing code that works just fine is silly IMHO.
5 commentaires
Guillaume
le 22 Nov 2018
How do I ge these into individual matricies?
You don't. Keep them as a single cell array and use indexing. It's probably the best. Instead of a, b, c, use mydata{1}, mydata{2} and mydata{3}.
If you really, really, really want to split it again into separe variables:
[a, b, c] = olddata{:};
Important, if mydata is a matrix, then mydata(i) is a single element of that matrix, one number. You cannot assign a whole matrix to a single element of a matrix, so
mydata(i) = olddata{i};
is never going to work. Furthermore, you need to learn how to access the elements of a cell array.
mydata = cell2mat(olddata(i));
is something that somebody that doesn't understand cell array indexing would write. The proper way of accessing the content of the ith element of a cell array is:
mydata = olddata{i}; %note the use of {} vs ().
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!