# How to merge two or more row vectors into one matrix on Matlab and Octave

To create a matrix from many row vectors in Matlab / Octave use the function vertcat()

vertcat(v1,v2 [,v3,...])

The v1 and v2 parameters are two or more row vectors.

Alternatively you can use the syntax

[v1;v2]

The output result is a two-row matrix.

## Examples

Example 1

Create two row vectors

>> v1=[1 2 3];
>> v2=[4 5 6];

Convert the two vectors into an array with the function vertcat()

>> vertcat(v1,v2)
ans =
1 2 3
4 5 6

The vertcat() function concatenates the vectors

The result is a two-row matrix.

$$\begin{pmatrix} 1 & 2 & 3 \end{pmatrix} \sqcup \begin{pmatrix} 4 & 5 & 6 \end{pmatrix} \Rightarrow \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}$$

Example 2

Concatenate vectors in an array using syntax [v1;v2]

>> [v1;v2]
ans =
1 2 3
4 5 6

The result is a matrix with three columns.

https://how.okpedia.org/en/matlab/how-to-merge-two-or-more-row-vectors-into-one-matrix-on-matlab-and-octave

Report us an error or send a suggestion to improve this page

Matrix in Matlab/Octave