Is there a quicker way than the following to 'flip' a true or false to its opposite state?
if x == true
x = false;
else
x = true;
end
Yes, perhaps only five lines of code is nothing to worry about but something that looks more like this would be fantastic:
x = flip(x);
You could do the following:
x = ~x;
u can use negation statement. I cant remember how it works in matlab, but i think is something like
x = ~x;
Franck's answer is better (using ~), but I just wanted to point out that the conditional in yours is slightly redundant. It's easy to forget that, since you already have a boolean value, you don't need to perform a comparison in your conditional. So you could have just done this...
if x
x = false;
else
x = true;
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With