10

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)';
Eitan T
  • 32,660
  • 14
  • 72
  • 109
G Gr
  • 6,030
  • 20
  • 91
  • 184

3 Answers3

33

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.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
6

I would imagine you could just transpose:

p = (normal(1:750)-1)'
Dan
  • 45,079
  • 17
  • 88
  • 157
  • Thanks Dan could not find that anywhere in the documentation! [previous question](http://stackoverflow.com/questions/13412283/matching-row-samples-to-class-labels) is why I asked. – G Gr Nov 16 '12 at 09:44
  • 1
    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. – Dan Nov 16 '12 at 09:47
  • 4
    If anyone is using complex numbers, note that the `'` (or `ctranspose()`) operator is the complex conjugate transpose. More info in the documentation here: https://www.mathworks.com/help/matlab/ref/ctranspose.html. If you want the nonconjugate transpose, use `.'` (or `transpose()`) instead. – jvriesem Oct 15 '17 at 20:45
5

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.

ojdo
  • 8,280
  • 5
  • 37
  • 60
Ru887321
  • 358
  • 3
  • 10