Skip to content
Advertisement

3D numpy array threshold comparison

I’m trying to solve whether or not each element in a 3d numpy array fits within a certain threshold. Currently I have nested for loops that get the job done, but it’s really slow and I don’t have all day to wait for my code to run LOL. The input spec is a little bit weird so I haven’t been able to find any more efficient solutions. Here’s an example:

input matrix:

JavaScript

upper threshold: 5

lower threshold:

JavaScript

and the output should be:

JavaScript

Advertisement

Answer

Broadcasting saves the day!

JavaScript

And now:

JavaScript

(Not sure if you wanted inclusive thresholds. In that case, use <= and >= for comparisons instead.)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement