Can you put a switch statement within an IF statement?
4 次查看(过去 30 天)
显示 更早的评论
I want two values to be set if two conditions are true. A and B are absolute decimal numbers. array(k,j+1) is either the value 12345 or a complex number with decimals. I tried:
if (A<B) && (array(k,j+1) == 12345)
B = A
C= k
end
Because this didn't work (not desired output), I tried an if statement within a switch statement:
switch array(k,j+1)
case 12345
if A<B
B=A
C=k
end
otherwise
;
end
This also didn't work (not desired output), so I tried:
if A < B
B=A
switch array(k,j+1)
case 12345
C= k
otherwise
;
end
end
It seems that none of these things give the desired output, has anyone and idea/suggestions?
0 个评论
回答(2 个)
Niels
2017-1-14
all off them work but not as you expect them to
i suppose A and B are matrices...
check the output of A<B and when it is "true", this should solve your problem
Image Analyst
2017-1-14
The first one should work if A & B are scalars. Because they didn't it's possible that array or the 12345 number may be floating point, double numbers, NOT integers. If they're not pure integers, like int32 or uint16 or whatever, or at least doubles with integer values, then you need to read and understand the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
If A & B are arrays and you want to test that every single value of A is less than the corresponding B at that location, and array and 12345 might be some floating point numbers then you can do
if all(A<B) && abs(array(k, j+1) - 12345) < someSmallToleranceNumber
B = A
C = k
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!