lettura simple

How to convert a matrix to a single vector in Octave

In this guide I'll explain how to transform a matrix into a single column vector.

I'll give you a practical example

Make a 2x3 matrix

>> M = [ 1 2 3 ; 4 5 6 ]
M =
1 2 3
4 5 6

Type M (:) to turn the matrix into a column vector

>> M(:)

This command takes all the elements of the matrix and arranges them in a column vector

ans =
1
4
2
5
3
6

If you want to get a single row vector, just transpose the vector by adding a superscript at the end

>> M(:)'

The result is a vector with the elements of the matrix arranged in a row

ans =
1 4 2 5 3 6




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin