Change row vector to column vector
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.
G Gr
New to C# and Matlab, enjoy learning via questions which makes stackoverflow my adopted home!
Updated on October 12, 2020Comments
-
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 over 11 yearsThanks Dan could not find that anywhere in the documentation! previous question is why I asked.
-
Dan over 11 yearssure 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 almost 11 yearsSo, a = 1:5 generates a row vector, but b=a(:) is a column vector?
-
user327301 over 10 yearsThis 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 about 7 years@TanWang Because this is how the programmers of MATLAB wanted it to work.
-
jvriesem over 6 yearsIf anyone is using complex numbers, note that the
'
(orctranspose()
) 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.'
(ortranspose()
) instead. -
Daddy Kropotkin about 5 yearsVery nice! This is exactly the thing I needed, and I could mirror @raoulcousins