concatenate cell array of cell arrays
조회 수: 35 (최근 30일)
이전 댓글 표시
I have got this cell variable with 10 cell arrays. It is possible to horizontally concatenate them if the dimension is consistent but if not it gives me an error. How do i concatenate this cell arrays of unequal length?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191074/image.png)
댓글 수: 0
채택된 답변
Stephen23
2018년 6월 7일
편집: Stephen23
2022년 2월 23일
Where C is your cell array, all of these are equivalent:
[C{:}]
horzcat(C{:})
cat(2,C{:})
댓글 수: 3
Stephen23
2018년 6월 7일
편집: Stephen23
2018년 6월 7일
Note that you could vertically concatenate them without any problem, as they all have exactly one column.
If you really need to horizontally concatenate them you will need to pad their rows first so that they have the same number of rows: you could use a loop, or cellfun. Or just do something like this:
col = size(C,2);
row = cellfun('size',C,1);
out = cell(max(row),col);
for k = 1:col
out(1:row(k),k) = C{k};
end
추가 답변 (1개)
Rishabh Rathore
2018년 6월 7일
편집: Rishabh Rathore
2018년 6월 7일
I'm assuming you want to retain the data of one cell array as a column.
A work around could be the following
- Find the length of the longest cell array
- append the empty cell to all the smaller cell arrays
- finally concatenate them, you may use horzcat for horizontal concatenation.
댓글 수: 0
참고 항목
카테고리
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!