# How to convert a matrix to a vector on Matlab and Octave

To convert a matrix into a vector in Matlab and Octave type M (:)

**M(:)**

This command takes all elements in the rows and columns of the matrix M as a single column vector.

## Example

Create a 2x2 matrix

>> M = [ 1 2 ; 3 4 ]

M =

1 2

3 4

To transform matrix M into a column vector type M (:)

The result is a column vector with 4 elements.

>> M(:)

ans =

1

3

2

4

Alternatively, type M(:)' to get a row vector

>> M(:)'

ans = 1 3 2 4

The final result is a row vector with four elements

https://how.okpedia.org/en/matlab/how-to-convert-a-matrix-to-a-vector-on-matlab-and-octave

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