Hi all,
I guess this is an easy one, but can't seem to find it. Suppose I have this function: def myFunction(x): result = None if x > 3: result = "some value" else: result = "another value" return result And I want to run it on an array a: b = myFunction(a) which then returns an array with "some value" and "another value". I could loop over the array, but I guess there's a better way to do this? (something like arrayMagic from Matlab if I'm not mistaken. thanks a lot, Pieter -- Pieter Cogghe Ganzendries 186 9000 Gent 0487 10 14 21 _______________________________________________ SciPy-user mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/scipy-user |
Hi,
I think you are looking for numpy.vectorize() It will not be faster than your own loop, but it will respect the shape of your array. Matthieu 2008/4/7, Pieter <[hidden email]>: Hi all, -- French PhD student Website : http://matthieu-brucher.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher _______________________________________________ SciPy-user mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/scipy-user |
In reply to this post by yuccaplant
Hi Pieter,
You could use something like : def myFunction(X): res = zeros(X.shape) res[where(X > 3)] = "some value" res[where(X <= 3)] = "another value" return res cheers, J-Pascal Projet PRIMA - Laboratoire LIG INRIA Grenoble Rhone-Alpes Research Centre 655, Avenue de l'Europe 38330 Montbonnot, France On 7-Apr-08, at 3:18 PM, Pieter wrote: > Hi all, > > I guess this is an easy one, but can't seem to find it. Suppose I have > this function: > > def myFunction(x): > result = None > if x > 3: > result = "some value" > else: > result = "another value" > return result > > And I want to run it on an array a: > b = myFunction(a) > > which then returns an array with "some value" and "another value". I > could loop over the array, but I guess there's a better way to do > this? (something like arrayMagic from Matlab if I'm not mistaken. > > thanks a lot, > > Pieter > > > > -- > Pieter Cogghe > Ganzendries 186 > 9000 Gent > 0487 10 14 21 > _______________________________________________ > SciPy-user mailing list > [hidden email] > http://projects.scipy.org/mailman/listinfo/scipy-user > _______________________________________________ SciPy-user mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/scipy-user |
or simply:
def myFunction(X): return where(X > 3, "some value", "other value") hth, L. On Mon, Apr 7, 2008 at 3:33 PM, J-Pascal Mercier <[hidden email]> wrote: Hi Pieter, -- Lorenzo Bolla [hidden email] http://lorenzobolla.emurse.com/ _______________________________________________ SciPy-user mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/scipy-user |
Hi,
Thanks for the answers, I think vectorize was the one I was looking for. My example function was really bad, I'm sorry for that. I'm actually reading an image and have to convert every pixel value to an 8 bit vector. thanks a lot, Pieter 2008/4/7, lorenzo bolla <[hidden email]>: > or simply: > > def myFunction(X): > return where(X > 3, "some value", "other value") > > hth, > L. > > > On Mon, Apr 7, 2008 at 3:33 PM, J-Pascal Mercier > <[hidden email]> wrote: > > > Hi Pieter, > > > > You could use something like : > > > > def myFunction(X): > > res = zeros(X.shape) > > res[where(X > 3)] = "some value" > > res[where(X <= 3)] = "another value" > > return res > > > > cheers, > > > > > > J-Pascal > > > > Projet PRIMA - Laboratoire LIG > > INRIA Grenoble Rhone-Alpes Research Centre > > 655, Avenue de l'Europe > > 38330 Montbonnot, France > > > > > > > > On 7-Apr-08, at 3:18 PM, Pieter wrote: > > > > > Hi all, > > > > > > I guess this is an easy one, but can't seem to find it. Suppose I have > > > this function: > > > > > > def myFunction(x): > > > result = None > > > if x > 3: > > > result = "some value" > > > else: > > > result = "another value" > > > return result > > > > > > And I want to run it on an array a: > > > b = myFunction(a) > > > > > > which then returns an array with "some value" and "another value". I > > > could loop over the array, but I guess there's a better way to do > > > this? (something like arrayMagic from Matlab if I'm not mistaken. > > > > > > thanks a lot, > > > > > > Pieter > > > > > > > > > > > > -- > > > Pieter Cogghe > > > Ganzendries 186 > > > 9000 Gent > > > 0487 10 14 21 > > > _______________________________________________ > > > SciPy-user mailing list > > > [hidden email] > > > http://projects.scipy.org/mailman/listinfo/scipy-user > > > > > > > _______________________________________________ > > SciPy-user mailing list > > [hidden email] > > http://projects.scipy.org/mailman/listinfo/scipy-user > > > > > > -- > Lorenzo Bolla > [hidden email] > http://lorenzobolla.emurse.com/ > _______________________________________________ > SciPy-user mailing list > [hidden email] > http://projects.scipy.org/mailman/listinfo/scipy-user > > -- Pieter Cogghe Ganzendries 186 9000 Gent 0487 10 14 21 _______________________________________________ SciPy-user mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/scipy-user |
Free forum by Nabble | Edit this page |