A compact way to horizontally concatenate rows of many cell arrays ?

11 Ansichten (letzte 30 Tage)
A compact way to horizontally concatenate rows of many cell arrays
a{1},a{2},a{3},..,a{N} % especially for N>>1
as in the following example ?
% Input
a{1} = {[0 0 1 4 1]
[9 9 0 0 0]
[2 3 5 1 2]};
a{2} = {[1 0 7 4 0]
[2 8 8 2 6]
[1 9 8 1 1]};
a{3} = {[7 7 6 0 6]
[9 0 9 1 2]
[3 9 2 4 6]};
a{4} = {[3 3 3 2 3]
[4 1 5 6 3]
[9 0 0 0 7]};
% A non-compact way to get my desired output, and that I would like to avoid or improve:
for i = 1 : 3
a2{i} = {[a{1}{i,:} a{2}{i,:} a{3}{i,:} a{4}{i,:}]};
end
c = vertcat(a2{:});
% Desired Output
c
c = 3×1 cell array
{[0 0 1 4 1 1 0 7 4 0 7 7 6 0 6 3 3 3 2 3]} {[9 9 0 0 0 2 8 8 2 6 9 0 9 1 2 4 1 5 6 3]} {[2 3 5 1 2 1 9 8 1 1 3 9 2 4 6 9 0 0 0 7]}
  1 Kommentar
Sim
Sim am 9 Feb. 2023
Bearbeitet: Sim am 9 Feb. 2023
The reason of my question is the following.
If I use the abovementioned way to build up the concatenation of cell arrays, when I have many cell arrays, the following piece of code
{[a{1}{i,:} a{2}{i,:} a{3}{i,:} .. a{N}{i,:}]} % for N>>1
becomes very long, and I guess I should write down all the cell arrays
a{j}{i,:} % where j = 1, 2, 3, ..., N
manually, right ?
Therefore, is there any way to build up that concatenation in a "more automatic" way when there are many (i.e. N>>1) cell arrays?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 9 Feb. 2023
a = {{[0,0,1,4,1];[9,9,0,0,0];[2,3,5,1,2]};{[1 0 7 4,0];[2,8,8,2,6];[1,9,8,1,1]};{[7,7,6,0,6];[9,0,9,1,2];[3,9,2,4,6]};{[3,3,3,2,3];[4,1,5,6,3];[9,0,0,0,7]}}
a = 4×1 cell array
{3×1 cell} {3×1 cell} {3×1 cell} {3×1 cell}
tmp = [a{:}];
c = tmp(:,1);
for k = 1:numel(c)
c{k} = [tmp{k,:}];
end
display(c)
c = 3×1 cell array
{[0 0 1 4 1 1 0 7 4 0 7 7 6 0 6 3 3 3 2 3]} {[9 9 0 0 0 2 8 8 2 6 9 0 9 1 2 4 1 5 6 3]} {[2 3 5 1 2 1 9 8 1 1 3 9 2 4 6 9 0 0 0 7]}

Weitere Antworten (1)

Rik
Rik am 9 Feb. 2023
Two calls to horzcat and a loop do the trick, although this can probably be improved a lot.
% Input
a{1} = {[0 0 1 4 1]
[9 9 0 0 0]
[2 3 5 1 2]};
a{2} = {[1 0 7 4 0]
[2 8 8 2 6]
[1 9 8 1 1]};
a{3} = {[7 7 6 0 6]
[9 0 9 1 2]
[3 9 2 4 6]};
a{4} = {[3 3 3 2 3]
[4 1 5 6 3]
[9 0 0 0 7]};
% A non-compact way to get my desired output, and that I would like to avoid or improve:
for i = 1 : 3
a2{i} = {[a{1}{i,:} a{2}{i,:} a{3}{i,:} a{4}{i,:}]};
end
c = vertcat(a2{:});
% Almost what you want
b = horzcat(a{:})
for row=1:size(b,1)
b{row,1} = horzcat(b{row,:});
end
b(:,2:end)=[]; % remove excess columns
%test equality
isequal(b,c)
ans = logical
1
  1 Kommentar
Sim
Sim am 9 Feb. 2023
Bearbeitet: Sim am 9 Feb. 2023
Dear @Stephen23, Dear @Rik, thanks a lot for your contributions!
Yes, the trick was made by
[a{:}] % or horzcat(a{:})
and
[tmp{k,:}] % or horzcat(b{row,:})
.......and I did not think about it! :-)
Please, consider that I would have accepted both answers, since they are very similar!
However, for this time, I will accept the @Stephen23's answer since it is slightly more compact than the @Rik's one. Indeed, it has not the @Rik's command to remove the excess columns:
b(:,2:end)=[]; % remove excess columns
Again, many many thanks to both of you for your time and contributions!!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by