I need to emulate the MATLAB find function, which returns linear indices for nonzero array elements. For instance:
>> a = zeros(4,4) a = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> a(1,1) = 1 >> a(4,4) = 1 >> find(a) ans = 1 16
numpy has a similar nonzero function, but returns a tuple of index arrays. For instance:
In [1]: from numpy import * In [2]: a = zeros((4,4)) In [3]: a[0,0] = 1 In [4]: a[3,3] = 1 In [5]: a Out[5]: array([[ 1., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 1.]]) In [6]: nonzero(a) Out[6]: (array([0, 3]), array([0, 3]))
Is there a function that gives me linear indices without calculating them myself?
source share