Matrix to Vector Conversion in Matlab
Solution 1
You could try transposing S and using (:)
S = S'
S_prime = S(:)
or for a row vector:
S_prime = S(:)'
Solution 2
Reshape takes the elements column wise so transpose S before reshaping.
>> reshape(S',1,[])
ans =
0 1 1 0 1 1 1 1
Solution 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.
Kiran
Hello, I am a full-time freelancer based in India. I am a self-taught software developer working on a number of technologies. Most of the time, I will be working in Python, Matlab, PHP/MySQL, C#, bash scripting. If you looking at my profile after seeing some of the questions/answers I have posted in this forum, I probably have the solution. Contact me at [email protected], I would be happy to help you. My interests include : Data Analysis, Data Science Data Mining, Web scraping (Have worked extensively with www.mca.gov.in, CIBIL, Bloomberg.com) API Development/Integration Blockchain I work here & here and can be reachable on Telegram here
Updated on June 14, 2022Comments
-
Kiran almost 2 years
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
-
Kiran about 13 yearsThis is not what I am looking for, This is similar to writing S(:) I am looking for row-wise concatenation into a vector.
-
Daniel Andersson about 13 yearsOK, that makes me wonder even more about the original problem, but just transpose S and then use the methods. I'll edit my answer.
-
Felix Ribeiro about 13 yearsI think the problem was that just to reshape S as you have given it returns [0 1 1 1 1 0 1 1] which is different as row wise selection rather than column wise selection was requested.
-
Kiran about 13 yearsThanks Adrian, Thanks a lot for your time and help. I wonder, why I didnot try taking the transpose. Thank you so much
-
Norbert Madarász about 11 yearsIs this S(:)' correct for a row vector? I think as a one liner, f'(:)' is the correct answer (at least testing with octave).
-
EIIPII over 10 yearshere is the octave documentation page about this feature: http://www.gnu.org/software/octave/doc/interpreter/Index-Expressions.html#Index-Expressions
-
sammy over 3 yearsI had the same experience as Norbert. S'(:)' gave a row vector. S(:)' gave a row vector, but the elements were added to it column-wise, not what I wanted.