Matrix to Vector Conversion in Matlab

纵然是瞬间 提交于 2019-12-10 13:48:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!