Concatenating cell arrays in matlab

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

Answers (1)

John D'Errico
John D'Errico on 22 Nov 2018
Edited: John D'Errico on 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 Comments

Thank you very much for this. it works perfectly! Yes, you were correct about the expected output. Must be a dodgy calculator!
Many thanks,
Phil
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?
In my opinion, it's much clearer and readable without the loop, but, to each their own:
z = cellfun(@vertcat, a, b, c, 'UniformOutput', false);
It also does not require any change if the numbers of elements in a,b,c changes. It may be slightly slower than the loop however.
Yes, that works superbly, and is indeed quite readable, though I think readable either way. My rule is not to bother looking to preoptimize code where I don't see an immediate better solution, and if I know the solution I have is already efficient. I'd give Guillaume a +1 for his response.
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
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 ().

Sign in to comment.

Categories

Asked:

on 22 Nov 2018

Commented:

on 22 Nov 2018

Community Treasure Hunt

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

Start Hunting!