Is this working right

3 visualizaciones (últimos 30 días)
Radoslav Gagov
Radoslav Gagov el 13 de Mzo. de 2017
Comentada: zehra ülgen el 18 de Oct. de 2020
Hey guys. I am trying to do this Assignment and i am pretty sure its correct but it keeps saing its now when i run the emulator that checks it.
Here is the assignemnt:
Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to 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. NOTE: Your function may not use any built-in functions, e.g., sort, min, max, median, etc.
my code
function [a,b,c] = sort3(v)
if v(1) >= v(2) && v(2) >= v(3)
a = v(1); b = v(2); c = v(3);
elseif v(1) >= v(3) && v(3) >= v(2)
a = v(1); b = v(3); c = v(2);
elseif v(2) >= v(1) && v(1) >= v(3)
a = v(2); b = v(1); c = v(3);
elseif v(2) >= v(3) && v(3) >= v(1)
a = v(2); b = v(3); c = v(1);
elseif v(3) >= v(1) && v(1) >= v(2)
a = v(3); b = v(1); c = v(2);
elseif v(3) >= v(2) && v(2) >= v(1)
a = v(3); b = v(2); c = v(1);
end
end
  1 comentario
Raji Prab
Raji Prab el 27 de Jul. de 2017
You need to have <= sign.

Iniciar sesión para comentar.

Respuestas (8)

Jan
Jan el 13 de Mzo. de 2017
Editada: Jan el 26 de Jul. de 2018
This can be written shorter with less chances for typos:
function [a, b, c] = sort3(v)
a = v(1);
b = v(2);
c = v(3);
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
end
function [b, a] = swap(a, b) % empty function body
end
Prefer one command per line in real code. I've move the if block to single lines only to emphasize the pattern optically here.

Adam
Adam el 13 de Mzo. de 2017
The question says to return them in 'non-decreasing' order. I ran your function with [1 2 3] and it returns 3, 2, 1 so you may just have your logic the wrong way round.

Radoslav Gagov
Radoslav Gagov el 13 de Mzo. de 2017
O right :D Thank you.

Steven Lord
Steven Lord el 13 de Mzo. de 2017
One way to increase the confidence that your code is doing what you expect is to pass in test cases for which you know the correct answer and check that you get the output you expected. Adam's test case of [1 2 3] is one example. Others include:
  • [2 3 1] (the input is not sorted already)
  • [1 2 1] (duplicate elements)
  • [3 3 3] (all the elements are duplicates)
  • [-1 0 -2] (negative numbers and zero)
  • [exp(1) pi sqrt(2)] (non-integer values)

Walter Fanka
Walter Fanka el 23 de Oct. de 2017
Editada: Walter Roberson el 17 de Oct. de 2020
function [s1,s2,s3] = sort3(v_3)
a = v_3(1); b = v_3(2); c = v_3(3);
if a <= b && a <= c s1 = a; if b <= c s2 = b; s3 = c; else s2 = c; s3 = b; end end
if b < a && b <= c s1 = b; if a <= c s2 = a; s3 = c; else s2 = c; s3 = a; end end
if c < a && c < b s1 = c; if a < b s2 = a; s3 = b; else s2 = b; s3 = a; end end
end
  1 comentario
Jan
Jan el 24 de Oct. de 2017
Something must be unnecessary here: If the first two conditions "a <= b && a <= c" and "b < a && b <= c" have been rejected before, it cannot be required to check for the third "c < a && c < b": If this third condition would be false, s1, s2, s3 would be undefined and the function would crash. So either this third condition is not needed, or the code fails for some input.
Do you see the bunch of warnings in the editor? Insert commas to calm down the MLint code checker:
if a <= b && a <= c, s1 = a; if b <= c, s2 = b; s3 = c; else, s2 = c; s3 = b; end, end
% ^ ^ ^ ^
Or better use line breaks:
if a <= b && a <= c
s1 = a;
if b <= c
s2 = b;
s3 = c;
else
s2 = c;
s3 = b;
end
end
I know, I have used multiple command per line also, but this was a bad example.

Iniciar sesión para comentar.


Vignesh M
Vignesh M el 4 de Mayo de 2018
Editada: Walter Roberson el 17 de Oct. de 2020
The question says to return them in 'non-decreasing' order. Your function is for decreasing order.
function [u1,u2,u3] = sort3(v)
if v(1) <= v(2) && v(2) <= v(3);
u1=v(1);u2=v(2);u3=v(3);
elseif v(1) <= v(3) && v(3) <= v(2);
u1=v(1);u2=v(3);u3=v(2);
elseif v(2) <= v(1) && v(1) <= v(3);
u1=v(2);u2=v(1);u3=v(3);
elseif v(2) <= v(3) && v(3) <= v(1);
u1=v(2);u2=v(3);u3=v(1);
elseif v(3) <= v(1) && v(1) <= v(2);
u1=v(3);u2=v(1);u3=v(2);
else v(3) <= v(2) && v(2) <= v(1);
u1=v(3);u2=v(2);u3=v(1);
end
  2 comentarios
Heirleking
Heirleking el 25 de Jul. de 2018
Are the commas after the if required? I had the same as you with different letters, when I erased them the simulator worked perfectly
Jan
Jan el 26 de Jul. de 2018
Editada: Jan el 18 de Oct. de 2020
@David Silva: There are no commas, but semicolons. They are not required. If you are in doubt, remove them and see what happens.
As described in a comment above, commas help, if you append the body of the if branch to the same line:
if a==b disp('match') end % ?!? Confusing!
if a==b, disp('match'); end % Better! Less confusion.
if a==b % Best! This is purely clear
disp('match')
end

Iniciar sesión para comentar.


Mohamed Ahmed Khedr
Mohamed Ahmed Khedr el 14 de Oct. de 2018
Your code working in a good way if you just make the following edit>>> change the arrangement of outputs
function [c,b,a] = sort3(v)

zehra ülgen
zehra ülgen el 17 de Oct. de 2020
Editada: Jan el 18 de Oct. de 2020
function A = sort3(x,y,z)
if x < y && x < z % x is the smallest one
if y < z
A = [x y z];
else
A = [x z y];
end
elseif y < x && y < z % y is the smallest one
if x < z
A = [y x z];
else
A = [y z x];
end
elseif z < x && z < y % z is the smallest one
if x < y
A = [z x y];
else
A = [z y x];
end
end
  2 comentarios
Jan
Jan el 18 de Oct. de 2020
The original question has a vector as input and wants 3 scalars as output.
Your function fails if two inputs are equal.
zehra ülgen
zehra ülgen el 18 de Oct. de 2020
Sorry, you right! I couldn't realize that these questions are not same.
"Write a function called sort3 that takes three unequal scalar arguments (the function does not have to check the format of the input or the inequality of the arguments). It uses if-statements, possibly nested, to return the three values of these arguments in a single row vector in increasing order, i.e., element one of the output vector equals the smallest input argument and element three of the output vector equals the largest input argument. NOTE: The function may not use the built-in function sort."

Iniciar sesión para comentar.

Categorías

Más información sobre Stochastic Differential Equation (SDE) Models en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by