I want to loop through the first dimension of an array, do a computation on each sub-array, and assign the result back to the array. It doesn't work when I use the "pythonic" form for b in bob: -- but I know that it ought to, because if I modify b in place it actually changes the matrix.
Is there a way to carry out the assignment in the middle "for" loop, below?
import numpy as np
bob = np.arange(0, 60).reshape((3, 4, 5))
print(bob)
# This does not change elements in bob
for b in bob:
b = b + 3
print(bob)
# This does change elements in bob
for b in bob:
b += 3
print(bob)
b = b + 3assigns a new object to the variableb. It does not modify the the originalb. This is a basic Python iterative behavior.b[:] = b+3should work like theb+=.