How to Iterate every 5th index over numpy array -


i want learn basic logic in python . have 2 numpy array . want subtract every 5th index 1 array another. far have tried below code:

x=np.arange(25,100).reshape(25,3) y=x[:,0] z=x[:,1] in range(0,25,5):        # till these 2 loop looks fine    print y[i] j in range(0,25,5):    print z[j] # problems portion in range(0,25,5):     j in range(0,25,5):           print y[i]-z[j]            -1  -16  -31  -46  -61   14       #output   -1  -16  -31  -46   29   14   -1  -16  -31   44   29   14   -1  -16   59   44   29   14   -1 

please suggest making mistake.why output above one? in advance !

you're missing simple beauty of numpy.

>>> y - z array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,    -1, -1, -1, -1, -1, -1, -1, -1]) 

to subtract every fifth position, use slice notation:

>>> y[::5] - z[::5] array([-1, -1, -1, -1, -1]) 

anyway, you're iterating on pairs instead of pairs @ same position. way, use 1 loop:

>>> in range(0,25,5): ...     print(y[i] - z[i]) -1 -1 -1 -1 -1 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -