How to sort a 3 element vector input to scalar output?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
JGraf
le 25 Fév 2017
Réponse apportée : Duddela Sai Prashanth
le 23 Sep 2018
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 commentaire
dpb
le 25 Fév 2017
Modifié(e) : dpb
le 25 Fév 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.
Réponse acceptée
Plus de réponses (2)
Jan
le 25 Fév 2017
Modifié(e) : Jan
le 26 Fév 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
- 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 commentaires
dpb
le 25 Fév 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).
Duddela Sai Prashanth
le 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
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!