[Numpy-discussion] numpy where function on different sized arrays
David Warde-Farley
d.warde.farley@gmail....
Sat Nov 24 15:19:27 CST 2012
M = A[..., np.newaxis] == B
will give you a 40x60x20 boolean 3d-array where M[..., i] gives you a
boolean mask for all the occurrences of B[i] in A.
If you wanted all the (i, j) pairs for each value in B, you could do
something like
import numpy as np
from itertools import izip, groupby
from operator import itemgetter
id1, id2, id3 = np.where(A[..., np.newaxis] == B)
order = np.argsort(id3)
triples_iter = izip(id3[order], id1[order], id2[order])
grouped = groupby(triples_iter, itemgetter(0))
d = dict((b_value, [idx[1:] for idx in indices]) for b_value, indices in
grouped)
Then d[value] is a list of all the (i, j) pairs where A[i, j] == value, and
the keys of d are every value in B.
On Sat, Nov 24, 2012 at 3:36 PM, Siegfried Gonzi
<sgonzi@staffmail.ed.ac.uk>wrote:
> Hi all
>
> This must have been answered in the past but my google search capabilities
> are not the best.
>
> Given an array A say of dimension 40x60 and given another array/vector B
> of dimension 20 (the values in B occur only once).
>
> What I would like to do is the following which of course does not work (by
> the way doesn't work in IDL either):
>
> indx=where(A == B)
>
> I understand A and B are both of different dimensions. So my question:
> what would the fastest or proper way to accomplish this (I found a solution
> but think is rather awkward and not very scipy/numpy-tonic tough).
>
> Thanks
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20121124/c6678a26/attachment.html
More information about the NumPy-Discussion
mailing list