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 commentaire

Guillaume
Guillaume le 7 Mai 2015
Modifié(e) : Guillaume le 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.

Connectez-vous pour commenter.

 Réponse acceptée

Thomas Koelen
Thomas Koelen le 7 Mai 2015
Modifié(e) : Thomas Koelen le 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 commentaires

Guillaume
Guillaume le 7 Mai 2015
Modifié(e) : Guillaume le 7 Mai 2015
In the OP post, A is a row vector, and B is a column vector. There's no way to concatenate both (without reshaping).
Gopalakrishnan venkatesan
Modifié(e) : Gopalakrishnan venkatesan le 7 Mai 2015
I have B as 3x1 and A as 1x2. I have to concatenate in that case. Is it possible?
thank you
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.
Shlomo David
Shlomo David le 22 Août 2024
What if it’s a different number of columns and of rows?

Connectez-vous pour commenter.

Plus de réponses (2)

Sabarinathan Vadivelu
Sabarinathan Vadivelu le 7 Mai 2015
Modifié(e) : Sabarinathan Vadivelu le 7 Mai 2015
Try this
A = {1,2};
B = {3, 5, 6};
C = horzcat(A, B)
ans =
C = {1 2 3 5 6}

3 commentaires

Guillaume
Guillaume le 7 Mai 2015
Modifié(e) : Guillaume le 7 Mai 2015
In the OP post, A is a row vector, and B is a column vector. There's no way to concatenate both (without reshaping).
Gopalakrishnan venkatesan
Modifié(e) : Gopalakrishnan venkatesan le 7 Mai 2015
I have B as 3x1 and A as 1x2. I have to concatenate in that case. Is it possible?
thank you
You should transpose one vector and then concatenate it.

Connectez-vous pour commenter.

singh
singh le 7 Mai 2015
Modifié(e) : singh le 7 Mai 2015
A = {1,2}, B={3;4;5}
vertcat([A]',B)
ans =
[1]
[2]
[3]
[4]
[5]

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by