Home > OS >  sum in numpy arrays with different dimension
sum in numpy arrays with different dimension

Time:03-31

I have a 3D numpy array with shape A=(10227,127,340) and a 1D with shape B=(10227), both array of float64. I just want to sum B to A(first column) at each of the 127x340 grid point.

The output array should be C(10227,127,340) with the values of the first column changed after the sum, of course.

CodePudding user response:

This may be one way of achieving it:

C = A   np.repeat(B, A.shape[1] * A.shape[2]).reshape(A.shape)

The following code also works but is much slower:

C = A.copy()
for i in range(A.shape[0]):
    C[i:]  = B[i]
  • Related