[Numpy-discussion] Vectorizing!!!!

Solimyr giannicristian@msn....
Sat Nov 19 06:23:39 CST 2011


Hi all!!!I'm pretty new here!!
I'm just use my free time to learn something more about python....I just
discovered a new word named 'Vectorizing'....
I'm going to explain my question...I have a matrix (no care about the size)
and I want made some mathematical operations like mean,standard
deviation,variance,ecc...

I post my result so can be usefull for all the newbie to understand the
vectorizing mean:

Mean using a 3x3 window and 2 FOR cicle (a=matrix of interest, b same size
then a but filled with zeros at the start):


        for i in range(1,row-1):
            for j in range(1,col-1):
                b[i][j]=
numpy.mean([a[i-1][j-1],a[i][j-1],a[i+1][j-1],a[i-1][j],a[i][j],a[i+1][j],a[i-1][j+1],a[i][j+1],a[i+1][j+1]])

Very disappointing in term of time..This is with the vectorizing(a=matrix of
interest, c same size then a but filled with zeros at the start):

       
c[1:row-1,1:col-1]=(a[0:row-2,0:col-2]+a[0:row-2,1:col-1]+a[2:row,2:col]+a[1:row-1,1:col-1]+a[1:row-1,0:col-2]+a[0:row-2,2:col]+a[1:row-1,2:col]+a[2:row,0:col-2]+a[2:row,1:col-1])/9


I have seen that I get a big advantage!!!But my question is:

If I want to calculate the variance in a 3x3 window, can I use the
vectorizing method and 'numpy.var' or I must explain the variance formula?

I don't know if the question is understandable! I have thought something
like:

c[1:row-1,1:col-1]=numpy.var(a[0:row-2,0:col-2]+a[0:row-2,1:col-1]+a[2:row,2:col]+a[1:row-1,1:col-1]+a[1:row-1,0:col-2]+a[0:row-2,2:col]+a[1:row-1,2:col]+a[2:row,0:col-2]+a[2:row,1:col-1])

But doesn't work because I think that numpy.var work over all the matrix and
not only in the 3x3 window!!Is it correct???

Thanks in advance for any answers!!!

Solimyr


-- 
View this message in context: http://old.nabble.com/Vectorizing%21%21%21%21-tp32868162p32868162.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.



More information about the NumPy-Discussion mailing list