필터 지우기
필터 지우기

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
  댓글 수: 1
Raji Prab
Raji Prab 2017년 7월 27일
You need to have <= sign.

댓글을 달려면 로그인하십시오.

답변 (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