python - Search a Numpy Array based on array index -
i have 2d numpy array , change of elements based on 2 criteria: first criteria condition. second criteria based on index of array (row , column number)
for instance take following code,
import numpy np #create 8x8 array = np.arange(64).reshape(8,8) condition = (a %2 ==0) b = np.where(condition,0,a) print b
this works don't want apply condition on entire domain of a. want apply condition on user-specified range of cells, first 3 rows , first 2 columns.
how can modify code accomplish ?
thanks! pk
edit : updated code based on mathdan's suggestion
import numpy np #create 8x8 array = np.arange(64).reshape(8,8) #create boolean conditional array condition = np.zeros_like(a,dtype='bool') #enforce condition on first 4x4 matrix condition[0:4, 0:4] = (a[0:4, 0:4] % 2 ==0) b = np.where(condition,0,a) print b
try (for example):
condition = np.zeros_like(a, dtype='bool') condition[0:2, 0:1] = (a[0:2, 0:1] % 2 ==0)
Comments
Post a Comment