Convert matrix to vector in matlab

I have a matrix MxN and I want to convert to a vector MNx1 with all the elements of a row from the matrix as elements of a vector.

I tried using reshape, but I was not successful.

Here is a small piece of code 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]

PS: Using a loop and concatenation is not an option, I'm sure there is a simple direct jump technique that I don't know about.

thank

+5
source share
3 answers

You can try transposing S and use (:)

S = S'
S_prime = S(:)

or for row vector:

S_prime = S(:)'
+8
source

Reshape accepts columns of elements so that they transpose S before changing.

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

ans =

     0     1     1     0     1     1     1     1
+4
source
reshape(S',1,prod(size(S)))

reshape(S',1,[])

But the question makes me wonder what your original problem is, and if this method is really part of the correct solution to the original problem.

+1
source

All Articles