How can I make the output of a function always a column vector, regardless of if the input is a column or row vector?
Mostrar comentarios más antiguos
I'm having trouble with a problem where I need the output to be a column vector, but inputs are sometimes provided as row vectors and other times as column vectors (so sometimes transposing it helps, but sometimes it flips a column vector to a row vector, making it wrong.) Thanks!
1 comentario
per isakson
el 7 de Feb. de 2018
Editada: per isakson
el 7 de Feb. de 2018
>> row = 1:12; % test data
>> row
row =
1 2 3 4 5 6 7 8 9 10 11 12
>> reshape( row, [],1 )
ans =
1
2
3
4
5
6
7
8
9
10
11
12
>>
or
>> row(:)
ans =
1
2
3
4
5
6
7
8
9
10
11
12
I think reshape is more readable.
Test this with a column vector
Respuesta aceptada
Más respuestas (1)
Robert
el 21 de Mzo. de 2022
You can use isrow for this:
if isrow(output)
output = output';
end
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!