# Concatenate two vectors with different dimensions and sort the result in an ascending order.

16 vues (au cours des 30 derniers jours)
Lama Hamadeh le 5 Avr 2023
Modifié(e) : MarKf le 5 Avr 2023
Hi,
If I have two vectors with different dimensions as following:
A = [ 1 ; 3 ; 6];
B = [ 2 ; 4 ; 5 ; 7, 0];
How can I vertically concatenate them and then sort their elements in an ascending order so the result would be as:
C = [0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7]
Thanks.
##### 2 commentairesAfficher AucuneMasquer Aucune
Stephen23 le 5 Avr 2023
@Lama Hamadeh: what have you tried so far?
Lama Hamadeh le 5 Avr 2023
I have tried "cat" but it only works for concatenating two vectors of similar dimensions.

Connectez-vous pour commenter.

### Réponses (2)

Luca Ferro le 5 Avr 2023
Modifié(e) : Luca Ferro le 5 Avr 2023
Be careful because you made a typo while defining B, the last element has a ',' instead of a ';'. B = [ 2 ; 4 ; 5 ; 7 >,< 0];
After correctly defining it, you should be able to do this:
A = [ 1 ; 3 ; 6];
B = [ 2 ; 4 ; 5 ; 7; 0];
C=sort(vertcat(A,B));
or even a shorter variation:
C=sort([A;B]);
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

MarKf le 5 Avr 2023
Modifié(e) : MarKf le 5 Avr 2023
vertcat(A,B) or cat(1,A,B) or just [A;B] is what you need to concatenate vertically. then just sort
Your code however gives the "concatenated not consistent" error because in B the last element is separated by a comma instead of a semicolon.
A = [ 1 ; 3 ; 6];
B = [ 2 ; 4 ; 5 ; 7; 0];
C = sort([A;B])
C = 8×1
0 1 2 3 4 5 6 7
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by