pythonarraysmultidimensional-arraydivide

How to divide an array of size (a,n,m) by an array of size (a,)?


I have two arrays: X of size (500, 10, 10) and Y with size (500,). I need to divide each (10x10) array in X by the corresponding element in Y.

for i in range(500):
    value[i] = X[i]/Y[i]

However, I get this error: ValueError: setting an array element with a sequence.

How can I do this division?


Solution

  • Assuming that you have numpy arrays: add additional axes to Y before division:

    new_arr = X / Y[:, None, None]