I want to remove the for loop from the below code. In my use case, the value of n will be typically much larger where the value in the ranges of 200-700 is not uncommon, and it is inconvenient to list them all down, and adding one more loop would only make this more inefficient.
import numpy as np
n = 3
imgs = np.random.random((16,9,9,n))
transform = np.random.uniform(low=0.0, high=1.0, size=(n,n))
for img in imgs:
for channel in range(img.shape[2]):
temp1 = img[:,:,0]
temp2 = img[:,:,1]
temp3 = img[:,:,2]
temp = temp1 * transform[channel][0] + temp2 * transform[channel][1] + temp3 * transform[channel][2]
img[:,:,channel] = temp/3
Any pointers will be gratefully appreciated.

imgandimagein your code (please copy and paste from your actual code) are the same thing. --- --- That said, you cannot vectorize your inner loop because, e.g.,temp1has an initial value in the first loop (whenchannel==0) and a different value at the end of the same loop, because you change the data segment of the arrayimg, andtemp1is just a view into that data segment. --- --- Is it possible that the code you posted is not doing what it's intended to do?imgandtransform.