0

I'd like to compare a signal with the help of np.all() with a reference signal. The reference can contain np.nan. At this NaN-position it does not matter if the signal fulfills the comparison condition. Following is a sample code. But this code does not work. I could compare each element within a for-loop but is there a smart way to do this comparison?

import numpy as np reference = np.array([np.nan, 1]) signal = np.array([2, 2]) print np.all(reference < signal) 

1 Answer 1

2

Limit the comparison to the non-nan values with a proper slice. You could also use np.finite instead of ~np.isnan

import numpy as np reference = np.array([np.nan, 1]) signal = np.array([2, 2]) idx = ~np.isnan(reference) print np.all(reference[idx] < signal[idx]) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, works great. But there is a small mistake in your code. Please change idx = ~np.isnan(signal) to idx = ~np.isnan(reference).
Oh, I'm sorry, I guess I didn't read your post carefully enough. I'm glad it helped, though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.