How can I make the output of a function always a column vector, regardless of if the input is a column or row vector?

조회 수: 140 (최근 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
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
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

추가 답변 (1개)

Robert
Robert 2022년 3월 21일
You can use isrow for this:
if isrow(output)
output = output';
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by