Matrix to vector

How to convert a matrix into a vector?

X=[1,4;...

2,5;...

3,6]

X =

1 4

2 5

3 6

a) using reshape

v=reshape(X,1,numel(X))

v =

1 2 3 4 5 6

b) simply by indexing 'all'

v=X(:)'

v =

1 2 3 4 5 6

% converting back to original 3 x 2 matrix

Y = reshape(v,3,2)

Y =

1 4

2 5

3 6

Row-wise extraction

By default, matrix elements are extracted column-wise, for row-wise extraction, simply use the transposed matrix X'

v=reshape(X',1,numel(X))

v =

1 4 2 5 3 6