Not displaying a 3 element vector as a result of my function? Any advice?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi! I am supposed to write a function that takes a 3-element vector as its sole arguments. It uses if- statements, possibly nested, to return a 3-element vector with its elements in non-decreasing order,and doesn't use any predefined functions. This is the code I have so far. It will display the lowest element, but not the other two. For example, if I make my V=[2 1 3], it will give ans=1. How do I make it display 1 2 3 in the correct order? Here's my code:
function [x, y, z]= mysort(V)
a=V(1);
b=V(2);
c=V(3);
if (a<=b && a<=c)
x=a;
if (b<=c)
y=b;
z=c;
else
y=c;
z=b;
end
end
if (b<=a && b<=c)
x=b;
if (a<=c)
y=a;
z=c;
else
y=c;
z=a;
end
end
if (c<=b && c<=a)
x=c;
if (b<=a)
y=b;
z=a;
else
y=a;
z=b;
end
end
end
0 件のコメント
採用された回答
Star Strider
2018 年 3 月 9 日
If you only ask for one output of a function that has more than one output, MATLAB will only return the first output. You have to ask for all of them in order to return all of them.
Your function works correctly. Try this:
V=[2 1 3];
[X,Y,Z] = mysort(V)
2 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!