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

3 views (last 30 days)
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 Comment
dpb
dpb on 25 Feb 2017
Edited: dpb on 25 Feb 2017
"... 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.

Sign in to comment.

Accepted Answer

JGraf
JGraf on 25 Feb 2017
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

More Answers (2)

Jan
Jan on 25 Feb 2017
Edited: Jan on 26 Feb 2017
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 Comments
dpb
dpb on 25 Feb 2017
">> 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 on 25 Feb 2017
Now I see what you mean - that helped. Thank you!

Sign in to comment.


Duddela Sai Prashanth
Duddela Sai Prashanth on 23 Sep 2018
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

Community Treasure Hunt

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

Start Hunting!