[Numpy-discussion] Any help from Numpy community?

Ryan rnelsonchem at gmail.com
Thu Mar 14 09:26:32 EDT 2013


> 
> Birda,
> 
> I think this will get you some of the way there:
> 
> import numpy as np
> x = ... # Here's your 2D atomic distance array
> # Create an indexing array
> index = np.arange( x.size ).reshape( x.shape )
> # Find the non-zero indices 
> items = index[ x != 0 ]
> # You only need the first half because your array is symmetric
> items = items[ : items.size/2]
> rows = items / x.shape[0]
> cols = items % x.shape[0]
> print 'Rows:   ', rows
> print 'Columns:', cols
> print 'Atomic Distances:', x[rows, cols]
> 
> Hope it helps.
> 
> Ryan
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion <at> scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 

Whoops. 
That doesn't quite work. You shouldn't drop half the items array like that. 
This will work better (maybe ?):

import numpy as np
x = ... # Here's your 2D atomic distance array
index = np.arange( x.size ).reshape( x.shape )
items = index[ x != 0 ]
rows = items / x.shape[0]
cols = items % x.shape[0]
# This index mask should take better advantage of the array symmetry
mask = rows < cols
print 'Rows:   ', rows[mask]
print 'Columns:', cols[mask]
print 'Atomic Distances:', x[rows[mask], cols[mask]]

Ryan





More information about the NumPy-Discussion mailing list