Asked by Claire
on 26 Nov 2014

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

Answer by Roger Stafford
on 26 Nov 2014

Accepted Answer

You can always do logical indexing using the negation of whatever condition you have.

t = wind(:,:,1)>0&wind(:,:,2)>0;

wind(~t) = NaN;

Answer by John D'Errico
on 26 Nov 2014

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

John D'Errico
on 26 Nov 2014

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.

Claire
on 26 Nov 2014

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.

Oleg Komarov
on 26 Nov 2014

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Oleg Komarov (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/164316-select-everything-not-returned-by-index#comment_251983

## Claire (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/164316-select-everything-not-returned-by-index#comment_251984

## John D'Errico (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/164316-select-everything-not-returned-by-index#comment_251996

Sign in to comment.