append cell array #2 to cell array #1 to get a final cell array of cells

조회 수: 42 (최근 30일)
Claire
Claire 2019년 10월 18일
댓글: Claire 2019년 10월 21일
Having trouble combining two arrays. Thanks for any advice.
A-- cell array #1: 1x184 (first 3 cells and last cell shown below)
4657x2 double 85x2 double 39x2 double.......87x2 double (last cell)
B --- cell array #2: 1x2
155x2 double 155x2 double
C -- #3 to also be a cell array 1x186 (desired)
4657x2 double 85x2 double 39x2 double.......87x2 double 155x2 double 155x2 double
I have tried --
C = cat(2,A,B);
C= [A;B]
C = horzcat(A,B); --> sometimes works. If I clear workspace, it will work if I run the code twice. Otherwise, it is unpredictable when it works and I get the error "Dimensions of arrays being concatenated are not consistent." How can I get this to work every time?
  댓글 수: 2
Adam
Adam 2019년 10월 18일
편집: Adam 2019년 10월 18일
horzcat should work fine, as should its equivalent:
C = [A B];
If it doesn't work every time there must be something you are missing out from your example, that occurs in other cases.
Claire
Claire 2019년 10월 18일
Cell array "A" is a subset of cell array "D"
A = (D(~cellfun('isempty',D)));
Further, cell array "D" is a subset of cell array "E". E is the original cell array.

댓글을 달려면 로그인하십시오.

채택된 답변

Kaashyap Pappu
Kaashyap Pappu 2019년 10월 21일
The operations carried out by either:
C = [A B];
C = horzcat(A,B);
C = cat(2,A,B);
Should work for your requirement. There is a chance that “A” could have more than 1 row based on the cell arrays “E” and “D”, which could create problems for concatenation. This could happen if "E" or "D" have more than 1 row.
Hope this helps!
  댓글 수: 1
Claire
Claire 2019년 10월 21일
Thank you for your comment. For some reason, A was alternating its dimensions after each iteration. For example, on iteration one A was 184x1. On iteration two, A was 1x184 (which is why the code always reliably worked on the second iteration).
I am unsure of why it was doing this, but I then put in a line to intentionally transpose both A and B to be vertical cell arrays and now I no longer have this error.
A = transpose (A);
B = transpose(B);
C = cat(1,A,B);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by