switch case syntax need
이전 댓글 표시
[EDIT: Wed May 25 04:59:27 UTC 2011 - Reformat - MKF]
I want a switch build a switch case statement as follows:
switch value
case {10-100}
disp('Method is within 100')
case {101-500}
disp('Method is within 500')
otherwise
disp('Unknown method.')
end
my problem is how to set case statement to check within the range 10 to 100 or 101 to 500. Thanks in advance.
채택된 답변
추가 답변 (1개)
Walter Roberson
2011년 5월 25일
switch value
case num2cell(10:100)
disp('Method is within 100')
case num2cell(101:500)
disp('Method is within 500')
otherwise
disp('Unknown method.')
end
댓글 수: 4
Mohammad Golam Kibria
2011년 5월 25일
Walter Roberson
2011년 5월 25일
If you want to handle real numbers, then why do you use the "otherwise" behavior for (value>100 & value<101) ? When we see obvious natural numbers in use, we are justified in providing a solution that only works for natural numbers.
Anyhow, here's the alternative:
switch true
case value >=10 && value <=100
disp('Method is within 100')
case value >=101 && value<=500
disp('Method is within 500');
otherwise
disp('Unknown method.')
end
david feilacher
2018년 9월 4일
genius !!
Walter Roberson
2018년 9월 4일
Note that for (value > 100 & value < 101) that you would hit the Otherwise. I would suggest that the problem was not created properly for real values:
switch true
case value >=10 && value <=100
disp('Method is within 100')
case value >100 && value<=500
disp('Method is within 500');
otherwise
disp('Unknown method.')
end
카테고리
도움말 센터 및 File Exchange에서 App Building에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!