Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a vector to be row vector?

What is the most simple way to force any vector to be a row vector?

I wish to have some function that transforms column vector to a row vector, and leaves a row vector unchanged. For example:

A= [1 2 3];
RowIt(A)

will output a row vector:

1 2 3

and:

B= [1; 2; 3];
RowIt(B) 

will output a row vector:

1 2 3

What is the most simple way to do it?

like image 437
Michael L. Avatar asked Dec 06 '22 10:12

Michael L.


1 Answers

To get any vector to be a row vector simply first force it to be a column vector, then transpose it:

A = rand(3,1);
B = A(:).'; % (:) forces it to unwrap along columns, .' transposes

A row vector gets unwrapped to a column vector and then transposed to a row vector again, and a column vector gets unwrapped to a column vector (thus doesn't change) and then gets transposed to a row vector.

Note that any N-dimensional matrix will be forced into a row vector this way due to column unwrapping:

A = rand(3,3,3,3);
B = A(:).'; % 81 element row vector
like image 171
Adriaan Avatar answered Dec 26 '22 10:12

Adriaan