masoud
masoud

Reputation: 31

using where to select a part of array

I have a 1D Array. we call grid_pro_x. it Shows the Position of particles. it is from -100 to 100 micro meter. I have also another 1D Array, Px_pro which is the energy of particles. I want to extract Px_pro array for those particles which their position is beyond 10 micrometer. I did something like that

len= len(grid_pro_x)
print len  ,' Number of initial X point'
<<<<320000

grid = np.where(grid_pro_x<10.e-06,0.,grid_pro_x) 
grid = np.extract(grid !=0,grid)
print np.shape(grid),' Number of elements grid'

<<<24000

np.where(grid_pro_x<10.e-6,0.,Px_pro)
Px_pro_new = np.extract(Px_pro !=0,Px_pro)
print np.shape(Px_pro_new),' Number of elements Px_pro-new'
<<<<320000

as you can see in the final line instead of 24000 I have 320000. can anyone help me what I have to do??

Thanks

Upvotes: 0

Views: 58

Answers (1)

Nils Werner
Nils Werner

Reputation: 36839

No need to use np.where().

numpy.abs(grid_pro_x) > 10e-6

will give you a boolean array marking which elements fulfill the condition. This array you can then use to extract the values from pro_x:

pro_x[numpy.abs(grid_pro_x) > 10e-6]

Upvotes: 1

Related Questions