I have 2D numpy array, I need two nested loops to iterate over each of its elements. I want to make some vectorization on the nested loops, but I keep getting an error saying,
j= np.arange (0,x.shape [1])
IndexError: tuple index out of range
These are the original nested loops:
for k in range(A.shape[0]):
for j in range(A.shape[1]):
A[k,j] = method1(x[k],x[j],a,c0,c1)
This is how I tried to make vectorization based on this answer, https://codereview.stackexchange.com/questions/17702/python-numpy-running-15x-slower-than-matlab-am-i-using-numpy-effeciently,
j= np.arange (0, A.shape [1])
for k in range(A.shape[0]):
A[k,j] = method1(x[k],x[j],a,c0,c1)
I tried to change the indices in np.arange, but it didn't work.
Can anyone please tell me how to fix this?
Thanks.
EDIT: By @ajcr comment, my mistake was in this line, j= np.arange (0,x.shape [1]), I was supposed to use the column count of the 2D array "A" as j= np.arange (0,A.shape [1]), but I have mistakenly used the 1D array x, hence the error. It's working perfectly now.
IndexErrorforx.shape[1]impliesxonly has one dimension - are you sure it's 2D?method1we can probably help you a lot more.