Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix to Vector Conversion in Matlab

Tags:

matlab

octave

I have a MxN Matrix and would like to convert into a vector MNx1 with all the elements of the row from the Matrix as the elements of the Vector.

I tried using reshape but I was not successful.

Here is the small code snippet and the expected result.

  S=[0     1
     1     0
     1     1
     1     1 ]

Expected Result:

S_prime= [ 0 1 1 0 1 1 1 1]

P.S: Using a loop and concatenation is not an option, I am sure there is a easy straight forward technique, which I am not aware.

Thanks

like image 856
Kiran Avatar asked Apr 28 '11 11:04

Kiran


2 Answers

You could try transposing S and using (:)

S = S'
S_prime = S(:)

or for a row vector:

S_prime = S(:)'
like image 115
dave85 Avatar answered Nov 15 '22 19:11

dave85


Reshape takes the elements column wise so transpose S before reshaping.

>> reshape(S',1,[])

ans =

     0     1     1     0     1     1     1     1
like image 4
Adrian Avatar answered Nov 15 '22 19:11

Adrian