I am trying to sort three numbers without using inbuilt functions.What is wrong with the function?
Show older comments
I am trying to sort three numbers without using inbuilt functions. The function takes a 3-element vector as its arguments and returns the 3 elements of the vector as 3 scalar output arguments in non-decreasing order.
function [small,medium,large] = sort_three(p)
if p(1)<=p(2)
if p(1)<=p(3)
small=p(1);
elseif p(1)>p(3)
medium=p(1);
small=p(3);
large=p(2);
return
end
elseif p(2)<=p(3)
if p(2)<=p(1)
small=p(2);
elseif p(2)>p(1)
small=p(1);
medium=p(2);
large=p(3);
return
end
else
small=p(3);
medium=p(2);
large=p(1);
return
end
1 Comment
per isakson
on 8 Dec 2016
I think you get the error because you have misunderstood how if-elseif-else-end works. See if, elseif, else
Accepted Answer
More Answers (0)
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!