1

So I'm fairly new to numpy and programming in general, and I was wonering if there is any way to change a numpy array through another array that is a slice, for example we have:

>>> import numpy as np
>>> a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
>>> a
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12, 13, 14, 15]])

And now I want multipy all values around and including '8' by 2, so I take a slice b and change it as such:

>>> y, x = 1, 2
>>> b = a[y-1:y+2, x-1:x+2]
>>> for i in range(len(b)):
        for x in range(len(b[i])):
            b[i][x] *= 2
>>> b
    array([[ 4,  6,  8],
           [14, 16, 18],
           [24, 26, 28]])

now I want to change these values in array a, how do I do so?

1 Answers1

0

Did you try:

 a[y-1:y+2, x-1:x+2] = a[y-1:y+2, x-1:x+2]*2
adir abargil
  • 5,495
  • 3
  • 19
  • 29