Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ismember not working for NaN

Tags:

nan

matlab

If I do

ismember([NaN 0 3 2],[0 NaN])

then the output is

0     1     0     0

where I obviously expected

1     1     0     0

I tried

ismember(['3' 0 3 2],[0 '3'])

then the output is

1     1     0     0

How can I make ismember work for NaN?

like image 269
user42459 Avatar asked Dec 18 '22 16:12

user42459


1 Answers

Following with the convention that NaN ~= NaN, ismember treats NaN values as distinct. A quick shim that works for your given use case would be:

>> ismembernan = @(a,b) ismember(a,b) | (isnan(a) & any(isnan(b)));
>> a = [NaN 0 3 2];
>> b = [0 NaN];
>> ismembernan(a,b)
ans =
     1     1     0     0
like image 105
TroyHaskin Avatar answered Jan 06 '23 17:01

TroyHaskin