MATLAB Answers

Sordin
0

Adjusting the dimensions of input argument

Asked by Sordin
on 6 May 2019
Latest activity Edited by Adam Danz
on 6 May 2019
I have written a function which takes in a row vector A as the input. If A is a column vector, I want the program to convert it into a row vector (i.e., replace it by its transpose). This can be achieved using an if statement:
if size(A,1) ~= 1
A = A';
end
Is there a shorter way of impelmenting this that does not require an if statement?
Any suggestions would be greatly appreciated.

  0 Comments

Sign in to comment.

Products


Release

R2019a

2 Answers

Answer by Stephen Cobeldick on 6 May 2019
 Accepted Answer

A = A(:).';

  0 Comments

Sign in to comment.


Answer by Adam Danz
on 6 May 2019
Edited by Adam Danz
on 6 May 2019

A = reshape(A,1,[]);
The above will transpose a column vector or will leave a row vector as is.
A quick note on the use of " ' " versus " .' " If your merely transposing a matrix, use the latter. The prior will also negate the sign of an imaginary parts of complex elements (if any).

  0 Comments

Sign in to comment.