if elseif else statement not working

Hello, I have just gotten back into matlab and I am practicing the else if statememts. this is currently what I am running:
function [L,w,o] = SortLargestNumber(input,input1,input2)
x = input;
c = input1;
m = input2
x
c
m
HighestNumber = 0;
if x>c && m
HighestNumber = x;
elseif c>x && m
HighestNumber = c;
else m>x && c
HighestNumber = m;
end
HighestNumber
end
I am not sure what I am doing wrong with the else if statement as I am only trying to sort which number is highest. can someone point out why and perhaps provide a better example of doing this? I know the variables are bad, its practice.
Thanks!
-Kie

댓글 수: 1

Image Analyst
Image Analyst 2021년 9월 29일
What are L, w, and o? You need to define the. o is a bad name by the way.

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

답변 (2개)

Dave B
Dave B 2021년 9월 29일
편집: Dave B 2021년 9월 29일

1 개 추천

You're writing "x is greater than c and m" and probably thinking of this as "x is greater than c, and x is greater than m". But MATLAB doesn't think of it this way, it sees "x is greater than c...and, unrelated, m" (it interprets that as m>0):
SortLargestNumber(10, 9, 8)
m = 8
x = 10
c = 9
m = 8
HighestNumber = 10
SortLargestNumber(1, 9, 8)
m = 8
x = 1
c = 9
m = 8
HighestNumber = 9
SortLargestNumber(1, 9, 80)
m = 80
x = 1
c = 9
m = 80
ans = logical
1
HighestNumber = 80
function [L,w,o] = SortLargestNumber(input,input1,input2)
x = input;
c = input1;
m = input2
x
c
m
HighestNumber = 0;
if x>c && x>m
HighestNumber = x;
elseif c>x && c>m
HighestNumber = c;
elseif m>x && m>c
HighestNumber = m;
end
HighestNumber
end

댓글 수: 2

Kieran Smith
Kieran Smith 2021년 9월 29일
Ohhhhhh, was the problem solely needing to edit the if statements to be "x>c && xm" etc? That's a good point if so, that did not cross my mind for some reason.
Dave B
Dave B 2021년 9월 29일
Yes, think of each thing between && and || as being totally independent. Also if your goal is to find the largest number, I fully agree with - just use max, but if your goal is to learn how to write a function that finds the largest number, some more feedback:
  • what about the cases where there are ties?
  • how would you extend this to 4 arguments?...the code will have to grow quite a bit with each additional value
  • Consider setting the initial value of HighestNumber to NaN, it might make it easier to notice an error.
  • What are the outputs of this function supposed to be?

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

David Hill
David Hill 2021년 9월 29일

1 개 추천

If-Else Method:
function largestNumber = SortLargestNumber(x,c,m)
if x>=c && x>=m
largestNumber = x;
elseif c>x && c>=m
largestNumber = c;
else
largestNumber = m;
end
Better method with built-in function:
largestNumber = max([x,c,m])

댓글 수: 1

Kieran Smith
Kieran Smith 2021년 9월 29일
Wow, that is way more efficient! thankyou for the insight!

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

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2021년 9월 29일

댓글:

2021년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by