[Numpy-discussion] numpy where function on different sized arrays

Siegfried Gonzi sgonzi at staffmail.ed.ac.uk
Sun Nov 25 04:22:58 EST 2012


On 25 Nov 2012, at 00:29, numpy-discussion-request at scipy.org wrote:
> 
> Message: 3
> Date: Sat, 24 Nov 2012 23:23:36 +0100
> From: Da?id <davidmenhur at gmail.com>
> Subject: Re: [Numpy-discussion] numpy where function on different
> 	sized	arrays
> To: Discussion of Numerical Python <numpy-discussion at scipy.org>
> Message-ID:
> 	<CAJhcF=2nmVeeQUo5Q45OUcQK+7KnQ_u0tvzUyOSXGpQbLSUy+Q at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> A pure Python approach could be:
> 
> for i, x in enumerate(a):
> 	for j, y in enumerate(x):
> 		if y in b:
> 			idx.append((i,j))
> 
> Of course, it is slow if the arrays are large, but it is very
> readable, and probably very fast if cythonised.


Thanks for all the answers. In that particular case speed is not important (A is 360x720 and b and c is lower than 10 in terms of dimension). However, I stumbled across similar comparison problems in IDL a couple of times where speed was crucial.

My own solution or attempt was this:

==
def fu(A, b, c):
    for x, y in zip(b,c):
        indx = np.where(A == x)
        A[indx] = y
    return A
==

> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121125/49bbc51a/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20121125/49bbc51a/attachment.ksh>


More information about the NumPy-Discussion mailing list