Concatenating cell arrays in matlab
조회 수: 4 (최근 30일)
이전 댓글 표시
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
답변 (1개)
John D'Errico
2018년 11월 22일
편집: John D'Errico
2018년 11월 22일
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
Guillaume
2018년 11월 22일
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 ().
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!