I have some electronic connectors with one being male and the other being female. The connectors have 5 coefficients associated with them, which have different numerical values for the males and females. I want to do some processing based on the sex of the connector. The 5 parameters are:
offsetDelayps
C0
C1
C2
C3
offsetDelayps should be 55.1 for a male connector, but 39.8 for a female. C0 for should be 50.389 for a male connector, but 53.004 for a female etc.
I want to set the appropriate values of the coefficients, based on whether the sex of the connector is male or female. So I tried this “If” statement, but after printing the value of offsetDelayps, it is always 55.1 (the male value) and never 39.8 (the female value). In the code below, the sex is set to female, so the If statement should evaluate to “False”, to the data after the comma (False values) should I think be set differently.
The values of 5 parameters are always set to the male values, and never the female ones, despite the “If” statement should evaluate to false when the sex is set to anything other than male. In the above code, offsetDelayps should, I believer, be set to 39.8 for female, but it is always set to 55.1, irrespective of what I set the sex to.
Any ideas what I’m doing wrong? Any ideas how to set the values properly?
Hi David, your problem stems from the use of symbols for male and female. Mathematica can’t know what kind of a value a symbol may aquire down the line, so sex==male is simply not determined. Try evaluating the following:
sex=female;
sex==male
sex===male
As you can see, the second line simply doesn’t know if your statement is True or False. The last line (with three equal signs, i.e. SameQ) does what you want because it treats undecided cases as False. However, the cleaner alternative would be to work with strings:
sex="female";
sex=="male"
This way, nothing can go wrong even if you accidentally assign a value to male or female eventually.
Thank you both. I was not aware of the === operator. I was only aware of the = and == operators.
I guess an advantage of the Switch command over the If command, is that if I make a typo, and mean to set the sex to male, but accidentally set it to mole (or some other typo), the variables will not get set, so all the processing will go wrong and it will be obvious there’s a problem.