Is this working right

2 次查看(过去 30 天)
Radoslav Gagov
Radoslav Gagov 2017-3-13
评论: zehra ülgen 2020-10-18
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

回答(8 个)

Jan
Jan 2017-3-13
编辑:Jan 2018-7-26
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 2017-3-13
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 2017-3-13
O right :D Thank you.

Steven Lord
Steven Lord 2017-3-13
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 2017-10-23
编辑:Walter Roberson 2020-10-17
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 个评论
Jan
Jan 2017-10-24
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
Vignesh M 2018-5-4
编辑:Walter Roberson 2020-10-17
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 个评论
Heirleking
Heirleking 2018-7-25
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 2018-7-26
编辑:Jan 2020-10-18
@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

请先登录,再进行评论。


Mohamed Ahmed Khedr
Mohamed Ahmed Khedr 2018-10-14
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 2020-10-17
编辑:Jan 2020-10-18
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 个评论
Jan
Jan 2020-10-18
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 2020-10-18
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."

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by