필터 지우기
필터 지우기

the problem of using 'case'

조회 수: 1 (최근 30일)
dcydhb dcydhb
dcydhb dcydhb 2019년 5월 13일
댓글: dcydhb dcydhb 2019년 5월 13일
i want to use a loop of omega and when the omega has diffreent value ,the m0,m1,m2...have the different value,so in the loop i use the 'case' as this
for omega=0.1:0.1:2;
switch omega
case 0.100000;
m0=0.031949;
m1=3.141268;
m2=6.283023;
m3=9.424670;
m4=12.566289;
m5=15.707898;
m6=18.849502;
m7=21.991102;
m8=25.132701;
m9=28.274298;
m10=31.415894;
case 0.200000;
m0=0.063931;
m1=3.140293;
m2=6.282536;
m3=9.424345;
m4=12.566046;
m5=15.707703;
m6=18.849339;
m7=21.990963;
m8=25.132579;
m9=28.274190;
m10=31.415797;
and there are still some codes,but in the result,when omega=0.2,it still use the m0,m1,m2...of case 0.100000;
so when i change it as
case abs(omega-0.200000)<0.01;
it still has the problem,and why,thanks a lot!!!
  댓글 수: 1
Stephen23
Stephen23 2019년 5월 13일
편집: Stephen23 2019년 5월 13일
Numbering variable names is a sign that you are doing something wrong. You should simply store all of those scalar numerics in one numeric array.

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

채택된 답변

Stephen23
Stephen23 2019년 5월 13일
편집: Stephen23 2019년 5월 13일
Your first (inadvisable, numerically fragile, should-be-avoided) concept works for me:
for omega = 0.1:0.1:2;
switch omega
case 0.1
disp('one')
case 0.2
disp('two')
end
end
displays this in the command window:
one
two
Because of floating pont error in those numbers, this would be an extremely unreliable way to write code. Do NOT do this!
Your second method can be done quite easily, by simply thinking about the actual case value that you actually want switch to compare (hint: your case conditions are equal to true or false, but omega is not equal to true or false):
for omega = 0.1:0.1:2;
switch true % this is the value for a valid CASE.
case abs(omega-0.1)<0.01;
disp('one')
case abs(omega-0.2)<0.01;
disp('two')
end
end
displays this in the command window:
one
two
  댓글 수: 3
Walter Roberson
Walter Roberson 2019년 5월 13일
When you use
switch numeric_value
case another_numeric_value
then the test is
if numeric_value == another_numeric_value
So let us follow that through with your proposed version with switch omega:
if omega == (abs(omega-0.1) < 0.01)
The right hand side is a logical value, false or true, convertable to numeric 0 or 1, and you want to compare that to omega. That can only possibly succeed if omega is 0 or 1 . In the case where omega is 1 then abs(1-0.1) is 0.9 and < 0.01 of that is false. In the case where omega is 0, then abs(0-0.1) is 0.1 and < 0.01 of that is false. Therefore switch omega cannot work.
Let us instead follow through with switch true:
if true == (abs(omega-0.1) < 0.01)
which can be true if omega is in the range approximately 0.1-0.01 to 0.1+0.01
dcydhb dcydhb
dcydhb dcydhb 2019년 5월 13일
great explanation and thank you all a lot!!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by