Change row vector to column vector

34,447

Solution 1

I would imagine you could just transpose:

p = (normal(1:750)-1)'

Solution 2

It is common practice in MATLAB to use the colon operator : for converting anything into a column vector. Without knowing or caring if normal is a row vector or a column vector, you can force p to be a column vector, like so:

p = p(:);

After this, p is guaranteed to be a column vector.

Solution 3

Setting

p = p(:);

is indeed the best approach, because it will reliably create column vector.

Beware of the use of the ' operator to do transpose. I have seen it fail dramatically many times. The matlab operator for non-conjugate transpose is actually .' so you would do:

p = p.'

if you want to do transpose without taking the complex conjugate.

Share:
34,447
G Gr
Author by

G Gr

New to C# and Matlab, enjoy learning via questions which makes stackoverflow my adopted home!

Updated on October 12, 2020

Comments

  • G Gr
    G Gr over 3 years

    How can I change this into a column, at the moment all 750 entries are on one row?

    p = normal(1:750)-1;
    

    I have tried:

    columns = 1;
    p = normal(1:750)-1;
    p = p(1:columns);
    

    I have also tried:

    rows = 1000;
    p = normal(1:750)-1;
    p = p(1:rows)';
    
  • G Gr
    G Gr over 11 years
    Thanks Dan could not find that anywhere in the documentation! previous question is why I asked.
  • Dan
    Dan over 11 years
    sure btw this is probably what you were trying in your first attempt: p = p(1:length(p), 1); but using ' to transpose is definitely the correct approach.
  • John
    John almost 11 years
    So, a = 1:5 generates a row vector, but b=a(:) is a column vector?
  • user327301
    user327301 over 10 years
    This is my preferred method, since it will not convert a column vector to a row vector if you already in fact had a column vector.
  • Eitan T
    Eitan T about 7 years
    @TanWang Because this is how the programmers of MATLAB wanted it to work.
  • jvriesem
    jvriesem over 6 years
    If anyone is using complex numbers, note that the ' (or ctranspose()) operator is the complex conjugate transpose. More info in the documentation here: mathworks.com/help/matlab/ref/ctranspose.html. If you want the nonconjugate transpose, use .' (or transpose()) instead.
  • Daddy Kropotkin
    Daddy Kropotkin about 5 years
    Very nice! This is exactly the thing I needed, and I could mirror @raoulcousins