Matrix to Vector Conversion in Matlab

16,207

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.

Share:
16,207
Kiran
Author by

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, 2022

Comments

  • Kiran
    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
    Kiran about 13 years
    This 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
    Daniel Andersson about 13 years
    OK, 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
    Felix Ribeiro about 13 years
    I 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
    Kiran about 13 years
    Thanks 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
    Norbert Madarász about 11 years
    Is this S(:)' correct for a row vector? I think as a one liner, f'(:)' is the correct answer (at least testing with octave).
  • EIIPII
    EIIPII over 10 years
  • sammy
    sammy over 3 years
    I 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.