2

I have something like this

import numpy as np 

array_3D =  np.random.rand(3,3,3) 
array_2D = np.random.randint(0, 3 , (3,3)) 

for i in range(3):
    for j in range(3): 
        array_3D[:, i, j][:array_2D[i, j]]=np.nan  

Is there a way to do this without the double for loop?

1 Answer 1

1

Create the mask with outer ranged-comparison and then assign -

mask = np.less.outer(np.arange(len(array_3D)), array_2D)  
array_3D[mask] = np.nan
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.