Entry
How do you keep track of elements of "ravel"ed Numeric arrays
Jul 5th, 2000 09:59
Nathan Wallace, Rob Hooft, unknown unknown, Hans Nowak, Snippet 93, Alex
"""
Packages: maths.numeric
"""
"""
Hi, again.
In case anyone else needs to do something like sorting an array and
keeping track of what belongs where in the array, I've found the
following function useful. It's not super-fast, but I guess it's doing
a lot.
"""
import operator
from Numeric import *
def flatten_indices (array):
index_array = indices (array.shape)
index_list = map (ravel, tuple (index_array))
index_list = apply (map, (None, ) + tuple (index_list))
values = map (operator.getitem, \
len (index_list) * [array], \
index_list)
return map (None, values, index_list)
"""
E.g.
>>> b = reshape (arange (4), (2, 2))
>>> flatten_indices (b)
[(0, (0, 0)), (1, (0, 1)), (2, (1, 0)), (3, (1, 1))]
>>>
"""