[Numpy-discussion] Finding a row match within a numpy array
Stefan van der Walt
stefan@sun.ac...
Sat Aug 18 05:25:21 CDT 2007
On Tue, Aug 14, 2007 at 11:53:03AM +0100, Andy Cheesman wrote:
> Dear nice people
>
> I'm trying to match a row (b) within a large numpy array (a). My most
> successful attempt is below
>
> hit = equal(b, a)
> total_hits = add.reduce(hit, 1)
> max_hit = argmax(total_hits, 0)
> answer = a[max_hit]
>
> where ...
> a = array([[ 0, 1, 2, 3],
> [ 4, 5, 6, 7],
> [ 8, 9, 10, 11],
> [12, 13, 14, 15]])
>
> b = array([8, 9, 10, 11])
>
>
>
> I was wondering if people could suggest a possible more efficient route
> as there seems to be numerous steps.
For large arrays, you may not want to calculate a == b, so you could
also do
[row for row in a if N.all(row == b)]
or find the indices using
[r for r,row in enumerate(a) if N.all(row == b)]
Cheers
Stéfan
More information about the Numpy-discussion
mailing list