How can I make the output of a function always a column vector, regardless of if the input is a column or row vector?
96 次查看(过去 30 天)
显示 更早的评论
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 个评论
per isakson
2018-2-7
编辑:per isakson
2018-2-7
>> 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
采纳的回答
possibility
2018-2-7
%Check if the output is a column vector
if size(output,2)>1
output=output(:)
end
Hope the problem is understood correctly.
Best
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!