English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Vector Concatenation

MATLAB allows you to concatenate vectors to create new vectors.

If you have two row vectors r1and r2, then to create a row vector r consisting of n elements and m elements, by adding these vectors, you can write-

r =[r1,r2]

You can also create matrix r by adding these two vectors, vector r2will be the second row of the matrix-

r =[r1;r2]

However, for this, both vectors should have the same number of elements.

Similarly, you can append two column vectors c1and c2, where n and m are the number of elements. To create a column vector c consisting of n elements plus m elements, by adding these vectors, you can write-

c =[c1; c2]

You can also create matrix c by adding these two vectors; vector c2will be the second column of the matrix-

c =[c1, c2]

However, for this, both vectors should have the same number of elements.

Online Examples

Create a script file using the following code-

r1 = [ 1 2 3 4 ];
r2 = [5 6 7 8 ];
r =[r1,r2]
rMat =[r1;r2]
 
c1 = [ 1; 2; 3; 4 ];
c2 = [5; 6; 7; 8 ];
c =[c1; c2]
cMat =[c1,c2]
When running the file, it displays the following result-
r =
Columns 1 through 7:
         1          2          3          4          5          6          7
Column 8:
         8
rMat =
         1          2          3          4
         5          6          7          8
c =
         1
         2
         3
         4
         5
         6
         7
         8
cMat =
         1          5
         2          6
         3          7
         4          8