问题
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
回答1:
You could try transposing S and using (:)
S = S'
S_prime = S(:)
or for a row vector:
S_prime = S(:)'
回答2:
Reshape takes the elements column wise so transpose S before reshaping.
>> reshape(S',1,[])
ans =
0 1 1 0 1 1 1 1
回答3:
reshape(S',1,prod(size(S)))
or shortcut
reshape(S',1,[])
But the question makes me wonder what your original problem is, and if this way really is part of the correct solution to the original problem.
来源:https://stackoverflow.com/questions/5817853/matrix-to-vector-conversion-in-matlab