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

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 comentario

dpb
dpb el 25 de Feb. de 2017
Editada: dpb el 25 de Feb. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

JGraf
JGraf el 25 de Feb. de 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

Más respuestas (2)

Jan
Jan el 25 de Feb. de 2017
Editada: Jan el 26 de Feb. de 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 comentarios

Thank you Jan,
I will more clear when asking a question. Function sort3 must take a 3-element vector as its sole arguments and return the three elements of the vector as three scalar output arguments in nondecreasing order, i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element.
So Vector 5, 4, 6 should return scalars as follows:
4
5
6
I have tried the following three things.
1) Create a vector called "test" and got below error.
>> test=[5,4,6]
test =
5 4 6
>> sort3(test) Not enough input arguments.
Error in sort3 (line 3) if a <=b && b<=c
2) Then I tried changing my function by adding"[ ]" to
function [out1,out2,out3]=sort3([a,b,c])
but then I got below error message.
>> sort3(test) Error: File: sort3.m Line: 1 Column: 33 Unbalanced or unexpected parenthesis or bracket.
3) Then I finally changed my code back to function [out1,out2,out3]=sort3(a,b,c) and my output is the way I want it to be. However my problem is that my input is not a vector but 3 scalars.
>> sort3(5,4,6)
out1 =
4
out2 =
5
out3 =
6
ans =
4
Hope this clarifies things.
">> 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 el 25 de Feb. de 2017
Now I see what you mean - that helped. Thank you!

Iniciar sesión para comentar.

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

Categorías

Más información sobre Genomics and Next Generation Sequencing en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Feb. de 2017

Respondida:

el 23 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by