Select everything NOT returned by index
33 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Claire
le 26 Nov 2014
Modifié(e) : Mallory Nation
le 25 Sep 2019
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 commentaires
John D'Errico
le 26 Nov 2014
Modifié(e) : John D'Errico
le 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?
Réponse acceptée
Roger Stafford
le 26 Nov 2014
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 commentaires
Plus de réponses (2)
John D'Errico
le 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
4 commentaires
Oleg Komarov
le 26 Nov 2014
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.
Mallory Nation
le 25 Sep 2019
Modifié(e) : Mallory Nation
le 25 Sep 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.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!