4

I have a logical array, isLand, that is true if the index is over land and false if the index is not over land, like the ocean. How can I easily create another logical array, isOcean, which is the inverse of isLand. All the ones in isLand will be zeros in isOcean and vice versa.

I know I can do this using a for loop but I feel there is a much better way.

2 Answers 2

11

Just use the logical NOT operator:

isOcean = ~isLand;

Easy-peasy lemon squeezy! ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, I was going to use the find function that this is much better
4

As gnovice tells you, ~ (the not operator) is the right answer of course, but you can also use

isOcean = isLand == 0;

This should work too:

isOcean = xor(1,isLand);

There are always several ways to solve any problem in MATLAB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.