Select everything NOT returned by index

Hello,
Apologies if this is a stupid question, but I haven't found a way to properly google it.
I have an index where certain conditions are met, but I want to make everything outside of that index NaN.
e.g.
x=find(wind(:,:,1)>0&wind(:,:,2)>0);
wind(x)=NaN; % But I actually want the parts not indexed to equal NaN
I'm looking for something like
wind(~x)=NaN
but that doesn't work. I can't just change my > sign around either because of the range of values in my matrix.
I'd just like to find a way to apply wind(x)=NaN to the values NOT in x.
Thanks, Claire

3 Commenti

x > 0 & y > 2 is logically equivalent of x <= 0 | y <= 2
Yes, but in my case it's not quite so simple. I have values in x between -1 and 1 and values in y between -1 and 1. I'm trying to isolate the area where both x and y are positive, and make the rest NaN. Simply swapping the > sign to a < sign will only then isolate where both x and y are negative.
I could always do it in two steps, but I can easily index to find the quadrant I want in one statement, and I was hoping it was just a case of selecting everything outside of that indexed quadrant to make it NaN.
John D'Errico
John D'Errico il 26 Nov 2014
Modificato: John D'Errico il 26 Nov 2014
Claire - read my comment. See that Oleg did as I did, changed the & to an or. It is time to go back to basic logical operator school. Um, maybe Venn diagrams?

Accedi per commentare.

 Risposta accettata

You can always do logical indexing using the negation of whatever condition you have.
t = wind(:,:,1)>0&wind(:,:,2)>0;
wind(~t) = NaN;

2 Commenti

Thank you. It seems my problem was with using "find" and when I changed it to "x=", the ~ operator worked.
Thanks! Claire
+1. The logical indexing is faster than the indirection over find.

Accedi per commentare.

Più risposte (2)

Why can't you change the inequalities?
x=find(wind(:,:,1)<=0 | wind(:,:,2) <= 0);
I'm listening, but your statement makes no sense as to why not. Basic logic tells us that:
~(A & B) == ~A | ~B
As trivially,
x=find(~(wind(:,:,1)>0 & wind(:,:,2)>0));
Finally, and equally trivially, but considerably less efficient because it is an extra and wholly unnecessary step, you might read up on what setdiff does.
doc setdiff

4 Commenti

My original statement isolates the areas where x and y are positive. If I simply reverse the > sign, then all that does is find where x and y are negative. It does not address the quadrants where x is positive but y is negative, and y is positive but x is negative.
I can select the quadrant I'm interested in with one step, but I then want to blank out the remaining three quadrants with NaNs. I was hoping it was just a simple matter of setting the values not in the index to NaN.
I'll look into setdiff and see if this will help.
Thanks.
No. You did not look at what I said. Read it again. See that I changed the & to an |, thus change an and to an or. I think you need to learn about logic, or at least review what I hope you may have learned some years ago.
You have the logical expression
A & B
but in reality, you wish the expression
not(A & B)
(The find operation is not relevant here. All that matters is the logical expression.) Essentially, you wish to negate what you found in that logical expression. So one simple solution is to simply add a ~ to the command that you had.
Back in high school mathematics (maybe it was earlier, a long time ago) we learned that
not(A & B) == not(A) | not(B)
This tells us that in order to find the negation of a pair of inequalities anded together, instead, we can change the > symbols to <=, and the and(&) to an or(|).
Do people not learn the basic calculus of logical operations anymore?
I showed you two ways to find what you needed, BOTH of which are completely valid. Yes, setdiff will also provide what you need, but it is completely unnecessary, as I stated, and is thus less efficient.
I simple, "you misunderstood my response" would have sufficed, without the additional backhanded insults.
Roger provided the answer to the question I had asked, so the problem is now sorted. Thanks.
Claire, you kept answering my comments and John's answer with your copy-paste text without even reading carefully. If there are two people telling you the same thing, and taking their time to do that in an elaborate clear way (John), it is not nice of you to paste the same sentences. I do not see much of an insult in the statement that you are missing the basics of logical truth tables (<http://en.wikipedia.org/wiki/Truth_table>) but an opportunity to go and check them out again, since much of programming is based on that.

Accedi per commentare.

Mallory Nation
Mallory Nation il 25 Set 2019
Modificato: Mallory Nation il 25 Set 2019
For fun, to do what was wanted using find, you could do this:
xNot = setdiff(x,1:length(wind(:,:,1)));
wind(:,:,xNot) = nan;
But as already stated, the best way is to not use find at all.

Community Treasure Hunt

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

Start Hunting!

Translated by