Concatenation inside cell arrays
37 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maad Ebrahim
am 12 Jun. 2018
Bearbeitet: Tejeswar Yarlagadda
am 13 Jun. 2020
Dear all,
I have a cell array that is made up of two cell arrays of (1x60) dimensions. Each one of them cell arrays of different dimensions. However, these dimensions are identical in both of them. Again, each one of these inner cell arrays contain multiple double matrices of the same size, but different from their counterparts in the other cell array with one dimension. What I have just said is illustrated for clarification in the image below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/191218/image.png)
What I would like to do is to concatenate the inner-most matrices each to its corresponding one along their first dimension (concat(17x12 & 13x12) => 30x12) as shown above. That will typically produce a single (1x60) cell array as shown above. I know I can do this with very complicated and time consuming loops, however, I would like your help to make it as compact as possible.
Yours, Maad
0 Kommentare
Akzeptierte Antwort
Jan
am 13 Jun. 2018
This is easy and efficient with a loop. I do not see, why you assume that it is "very complicated and time consuming".
% Input cells: A, B
C = cell(size(A));
for k = 1:numel(C)
a = A{k};
b = B{k};
ab = cell(size(a));
for kk = 1:numel(ab)
ab{kk} = [a{kk}, b{kk}];
end
C{k} = ab;
end
3 Kommentare
Jan
am 13 Jun. 2018
You are welcome. The pictures in your question are excellent. The problem is hard to explain in words, but it was easy to write the code just based on these images. +1 for your question.
Weitere Antworten (1)
Tejeswar Yarlagadda
am 12 Jun. 2020
Bearbeitet: Tejeswar Yarlagadda
am 13 Jun. 2020
I hope this might help you
let us say A is sub cell of B and A is again having single layer of sub cells
for example
A = {{[1;2;3];[5;6;7]},{[1;2;3],[5;6;7]};{[1,2,3];[5,6,7]},{[1,2,3],[5,6,7]}};
if you want to concatenate in
first direction
cellfun(@(x) cat(1,x{:}),A,'UniformOutput',false)
ans =
2×2 cell array
{6×1 double} {6×1 double}
{2×3 double} {2×3 double}
second direction
cellfun(@(x) cat(2,x{:}),A,'UniformOutput',false)
ans =
2×2 cell array
{3×2 double} {3×2 double}
{1×6 double} {1×6 double}
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!