Concatenating cell arrays in matlab

2 visualizaciones (últimos 30 días)
Phil Roberts
Phil Roberts el 22 de Nov. de 2018
Comentada: Guillaume el 22 de Nov. de 2018
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

Respuestas (1)

John D'Errico
John D'Errico el 22 de Nov. de 2018
Editada: John D'Errico el 22 de Nov. de 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 comentarios
Phil Roberts
Phil Roberts el 22 de Nov. de 2018
Hello again,
Now I have the output as:
olddata =
{61595×155 double} {61595×155 double} {61595×155 double}
How do I ge these into individual matricies?
I have tried:
for i = 1:3
mydata(i) = cell2mat(olddata(i))
end
The above does not work but the following does, for one of the cells anyway:
mydata = cell2mat(olddata(1))
Any help is greatly appreciated.
Many thanks,
Phil
Guillaume
Guillaume el 22 de Nov. de 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 ().

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by