python - Counting number that falls in a bin based on coordinates -


i trying count number of molecules falls particular bin based on coordinate of molecule. think nonzero option of numpy (similar find() in matlab job). @ first, did not use np.any got error

valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all().

when used np.any, error persist.

lower limit = 0 bin_size=0.01 box=40 no_bin= box/bin_size k in range(no_bins):         if k==0:             count = np.any(np.size(np.nonzero(data_matrix[:,3]>= k*bin_size+lower_limit , \             data_matrix[:,3]<=(k+1)*bin_size+lower_limit)))         else:             count = np.any(np.size(np.nonzero(data_matrix[:,3]>=(k-1)*bin_size+lower_limit , \             data_matrix[:,3]<=k*bin_size+lower_limit)))              atom_in_bin[j,2] = atom_in_bin[j,2] + count 

first thing, use float value in range, function takes int arguments. can cast no_bins int, advise build bins np.arange , iterate on :

bins = np.arange(0, box, bin_size) k, bin in enumerate(bins): 

your error comes , in np.any. , operation not define arrays more 1 element. instead have use np.logical_and function :

np.any(np.logical_and(data <= top_bin, data > bot_bin)) 

but if trying build histogram, numpy has function : https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html.

bins = np.arange(0, box + bin_size, bin_size) hist, _ = np.histogram(data, bins=bins) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -