How to sort a 3 element vector input to scalar output?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(2 个)
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
- if the 1st element is greater than the 2nd, swap them.
- if the 2nd is now grater than the 3rd, swap them.
- 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
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).
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!