Combine two cell array of different dimension
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Gopalakrishnan venkatesan
am 7 Mai 2015
Kommentiert: Shlomo David Sherer
am 22 Aug. 2024
I have a two cell array A = {1,2}, B={4;5;6}
I need the result as one single array C = { 1 2 4
5
6 }
How it can be done?
Thank you
1 Kommentar
Guillaume
am 7 Mai 2015
Bearbeitet: Guillaume
am 7 Mai 2015
If your cell arrays only contain scalar values, why are you bothering with cell arrays instead of the much faster and easier to use plain matrices?
Furthermore, it's unclear what output you want. Note that:
C = { 1 2 4
5
6 }
is not valid matlab syntax.
Akzeptierte Antwort
Thomas Koelen
am 7 Mai 2015
Bearbeitet: Thomas Koelen
am 7 Mai 2015
A={1,2};
B={4,5,6};
C=cat(2,A,B)
C =
[1] [2] [4] [5] [6]
2 in cat is the direction you want to concatenate in, 1 does it vertically, which doesn't work because the cell doesn't have the same number of columns!
4 Kommentare
Thomas Koelen
am 7 Mai 2015
O, I didn't seee that! Must've thought it was a typo.
What you can do is:
A={1,2};
B={4;5;6};
C=cat(2,A,B')
C =
[1] [2] [4] [5] [6]
Like Guillaume said, there is no way to make a matrix or cell that's not a rectangle.
Weitere Antworten (2)
Sabarinathan Vadivelu
am 7 Mai 2015
Bearbeitet: Sabarinathan Vadivelu
am 7 Mai 2015
Try this
A = {1,2};
B = {3, 5, 6};
C = horzcat(A, B)
ans =
C = {1 2 3 5 6}
3 Kommentare
Siehe auch
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!