Is this working right

on 13 Mar 2017
Latest activity Answered by Mohamed Ahmed Khedr

Mohamed Ahmed Khedr (view profile)

on 14 Oct 2018
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

Raji Prab

Raji Prab (view profile)

on 27 Jul 2017
You need to have <= sign.

on 13 Mar 2017
Edited by Jan

Jan (view profile)

on 26 Jul 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.

on 13 Mar 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.

on 13 Mar 2017

O right :D Thank you.

Steven Lord (view profile)

on 13 Mar 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 (view profile)

on 23 Oct 2017

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

Jan

Jan (view profile)

on 24 Oct 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.

Vignesh M (view profile)

on 4 May 2018
Edited by Vignesh M

Vignesh M (view profile)

on 4 May 2018

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

David Silva

David Silva (view profile)

on 25 Jul 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 (view profile)

on 26 Jul 2018
@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 % ?!? Conusing!
if a==b, disp('match'); end % Better! Less confusion.
if a==b % Best! This is purely clear
disp('match')
end