Can you put a switch statement within an IF statement?

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?

Réponses (2)

Niels
Niels le 14 Jan 2017

0 votes

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

1 commentaire

I should have said, yeah, all of them work but not as I want. A and B are absolute decimal values. Will try the "true" thing :)!

Connectez-vous pour commenter.

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

9 commentaires

Errrr A, B are absolute values with decimal places and array(k,j+1) is either a complex number with decimals or the value 12345. Will think about what you've written and read the FAQ. Thanks!
Then this will probably work:
if A < B && abs(array(k, j+1) - 12345) > 0.0001
B = A
C = k
end
You'll enter the if block if A is less than B AND array is not 12345 (no matter whether it's complex or real). Both conditions must be true. If just one is true then it will not enter the if block.
It says that C is an undefined variable. k is an integer; so I suppose before the if statement, I have to declare that C is an integer... Something like C = int(k)? Not sure this makes sense.
nope, you dont have to declare C
you would only have to, if you set another variable to C, like
Case= C; % and C never used or declared before
Yeah, thought so; the program ran before but when I try the >0.0001 suggestion, it has a problem with variable C. Mhmm, interesting.
can you maybe give us the values of A, B ans array(k, j+1)?
What problem? You forgot to attach the error message. You're simply setting C equal to k, and k definitely exists or else you would have had trouble referencing array(k, j+1). I can't imagine any problem with setting C equal to k, so show your actual code, not this pseudocode.
So, the whole overview is a lot more complicated. So I have a function which generates an N X sz array. I reorder these values and put them into a newarray. The first column of both the arrays are the same. And then it goes through a series of loops. But the general idea is, you look at a value, and look in the previous column, to see which value is the closest. Then you do the same for every value comparing to the previous column. BUT, if a value in the previous column has already been matched with something, you have to pick the second closest value, which is why I posted this question on here. For more clarity see link (note, A = Distance, B = BestDistance, C = BestMatch):
I've tried so many ways to do this, but the simplest (logically in my head) seems to be prefill the newarray with an improbable number that doesn't appear in the original array and use an if statement to say, if a particular value is the closest value AND the newarray is not filled yet, then fill it. I've probably explained that terribly, but hope it makes some sense... ! ( :
belle
belle le 14 Jan 2017
Modifié(e) : belle le 14 Jan 2017
The error message is "Undefined function or variable "BestMatch". But this error message doesn't appear when I run the 3 code snippets in my original question. However, none of the 3 codes is doing what I thought it would be doing. The actual code is really long so I think it's better to try and highlight where the problem is rather than code dump.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 14 Jan 2017

Modifié(e) :

le 14 Jan 2017

Community Treasure Hunt

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

Start Hunting!

Translated by