I'm trying to figure out a good way of doing the following addition operation without using np.repeat
to create a large dimension. If using np.repeat
and adding is the best solution let me know.
I'm also confused about what broadcasting is doing in this case. Essentially I have a 4d matrix, and I want to add a 2d matrix in the 1st and 2nd index, while and doing this across index 0 and index 3.
This works correctly
a = np.arange(64).reshape((2,4,4,2)).astype(float)
b = np.ones((2,2))
a[:, 0:2, 0:2, : ] += b
This throws an error. What is a good way of doing this?
a[:, 0:3, 0:3, :] += np.ones((3,3))
This works but is not what I'm looking to do
c = np.arange(144).reshape(3,4,4,3).astype(float)
c[:, 0:3, 0:3, :] += np.ones((3,3))