How to sort a 3 element vector input to scalar output?

조회 수: 7 (최근 30일)
JGraf
JGraf 2017년 2월 25일
답변: Duddela Sai Prashanth 2018년 9월 23일
Hi,
My function sort3 is supposed to take a 3-element vector and return the vector as three scalar output in nondecreasing order. My function may not use any built-in functions. My function seems to sort correctly. However, if I input a vector [a,b,c] it doesn't work. And if I change the function to function
(out1,out2,out3)=sort3[a,b,c]
it doesn't work either.
Also, at the end of my result, the function keeps giving me "ans =". I thought I eliminated that by having the semicolons.
Thank a lot
function [out1,out2,out3]=sort3(a,b,c)
if a<=b && b<=c
out1=a
out2=b
out3=c
elseif a<=c && c<=b
out1=a
out2=c
out3=b
elseif c<=a && a<=b
out1=c
out2=a
out3=b
elseif b<=a && a<=c
out1=b
out2=a
out3=c
elseif b<=c && c<=a
out1=b
out2=c
out3=a
else
out1=c
out2=b
out3=a
end
  댓글 수: 1
dpb
dpb 2017년 2월 25일
편집: dpb 2017년 2월 25일
"... eliminated that by having the semicolons."
There isn't a semicolon to be seen in the entire function.?
As you've written it, the function expects three inputs, not an array/vector.
It does no error checking on those inputs to verify this, however.

댓글을 달려면 로그인하십시오.

채택된 답변

JGraf
JGraf 2017년 2월 25일
I figured it out as follows:
function [out1,out2,out3]=sort3(v)
if v(1) <=v(2) && v(2)<=v(3);
out1=v(1)
out2=v(2)
out3=v(3)
elseif v(1)<=v(3) && v(3)<=v(2);
out1=v(1)
out2=v(3)
out3=v(2)
elseif v(3)<=v(1) && v(1)<=v(2);
out1=v(3)
out2=v(1)
out3=v(2)
elseif v(2)<=v(1) && v(1)<=v(3);
out1=v(2)
out2=v(1)
out3=v(3)
elseif v(2)<=v(3) && v(3)<=v(1);
out1=v(2)
out2=v(3)
out3=v(1)
else
out1=v(3)
out2=v(2)
out3=v(1)
end

추가 답변 (2개)

Jan
Jan 2017년 2월 25일
편집: Jan 2017년 2월 26일
Please do not write only "does not work", but explain, what you observe with details. This helps to understand the problem.
(out1,out2,out3)=sort3[a,b,c] fails, because you need round parenthesis to call a function and square brackets to collect the output:
[out1,out2,out3] = sort3(a,b,c)
Would you sort the array in the same way, when you do this manually? Imagine you have 3 apples of different size on the table. Then you do not need 6 comparisons, but up to 3 swaps:
function [a,b,c] = sort3(a,b,c)
if a > b
[a, b] = swap(a, b);
end
if b > c
[b, c] = swap(b, c);
end
if a > b
[a, b] = swap(a, b);
end
function [b, a] = swap(a, b)
% empty function body
  1. if the 1st element is greater than the 2nd, swap them.
  2. if the 2nd is now grater than the 3rd, swap them.
  3. now the 2nd might be smaller than the fist, and if so, swap them.
Now the 3 elements must be in ascending order.
The swap() function does not need a body: the input elements are replied in reverse order.
[EDITED] Considering the comment, that a vector is wanted as input:
function [a,b,c] = sort3(v)
a = v(1); b = v(2); c = v(3);
... The same as above
  댓글 수: 3
dpb
dpb 2017년 2월 25일
">> sort3(test) Not enough input arguments."
Precisely what I told you first posting; your function definition as written requires three scalar inputs, not a single 3-vector.
Try
doc function
and then look at the first example "Function with One Output". While it specifically mentions the one output, it also uses one input that is a vector.
Of course, when you correct this, it will lead to a bunch of other subsequent errors because none of your code is written to address a vector argument. (Hint: you'll need subscripts).
JGraf
JGraf 2017년 2월 25일
Now I see what you mean - that helped. Thank you!

댓글을 달려면 로그인하십시오.


Duddela Sai Prashanth
Duddela Sai Prashanth 2018년 9월 23일
function [a,b,c]= sort3(V)
if V(1) >= V(2) && V(1) >= V(3)
if V(2) >= V(3)
c = V(1); b = V(2); a = V(3);
else
c = V(1); a = V(2); b = V(3);
end
elseif V(2) >= V(3) && V(2) >= V(1)
if V(1) >= V(3)
c = V(2); b = V(1); a = V(3);
else
c = V(2); b = V(3); a = V(1);
end
elseif V(3) >= V(2) && V(3) >= V(1)
if V(1) >= V(2)
c = V(3); b = V(1); a = V(2);
else
c = V(3); b = V(2); a = V(1);
end
end

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by