python - Unable to retrieve required indices from multiple NumPy arrays -


i have 4 numpy arrays of same shape(i.e., 2d). have know index of last array (d) elements of d smaller 20, indices of d should located in region elements of array(a) 1; , elements of array (b) , (c) not 1.

i tried follows:

mask = (a == 1)|(b != 1)|(c != 1)  answer = d[mask | d < 20] 

now, have set regions of d 1; , other regions of d 0.

d[answer] = 1 d[d!=1] = 0 print d 

i not solve problem. how solve it?

import numpy np  = np.array([[0,0,0,1,1,1,1,1,0,0,0],                  [0,0,0,1,1,1,1,1,0,0,0],                  [0,0,0,1,1,1,1,1,0,0,0],                  [0,0,0,1,1,1,1,1,0,0,0],                  [0,0,0,1,1,1,1,1,0,0,0],                  [0,0,0,1,1,1,1,1,0,0,0]])  b = np.array([[0,0,0,1,1,0,0,0,0,0,0],                  [0,0,0,0,0,0,1,1,0,0,0],                  [0,0,0,1,0,1,0,0,0,0,0],                  [0,0,0,1,1,1,0,1,0,0,0],                  [0,0,0,0,0,0,1,0,0,0,0],                  [0,0,0,0,1,0,1,0,0,0,0]])  c = np.array([[0,0,0,0,0,0,1,0,0,0,0],                  [0,0,0,0,0,0,0,0,0,0,0],                  [0,0,0,0,0,0,1,1,0,0,0],                  [0,0,0,0,0,0,1,0,0,0,0],                  [0,0,0,0,1,0,0,0,0,0,0],                  [0,0,0,0,0,1,0,0,0,0,0]])   d = np.array([[0,56,89,67,12,28,11,12,14,8,240],                  [1,57,89,67,18,25,11,12,14,9,230],                  [4,51,89,87,19,20,51,92,54,7,210],                  [6,46,89,67,51,35,11,12,14,6,200],                  [8,36,89,97,43,67,81,42,14,1,220],                  [9,16,89,67,49,97,11,12,14,2,255]]) 

the conditions should and-ed together, instead of or-ed. can first boolean array / mask representing desired region, , modify d based on it:

mask = (a == 1) & (b != 1) & (c != 1) & (d < 20) d[mask] = 1 d[~mask] = 0 print d 

output:

[[0 0 0 0 0 0 0 1 0 0 0]  [0 0 0 0 1 0 0 0 0 0 0]  [0 0 0 0 1 0 0 0 0 0 0]  [0 0 0 0 0 0 0 0 0 0 0]  [0 0 0 0 0 0 0 0 0 0 0]  [0 0 0 0 0 0 0 1 0 0 0]] 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -