Adjusting the dimensions of input argument

11 次查看(过去 30 天)
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';
Is there a shorter way of impelmenting this that does not require an if statement?
Any suggestions would be greatly appreciated.


Stephen23 2019-5-6
A = A(:).';

更多回答(1 个)

Adam Danz
Adam Danz 2019-5-6
编辑:Adam Danz 2019-5-6
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).


Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by