Say I have the following array a and subset 2x2 matrices from it:
import numpy as np
np.random.seed(0)
a = np.random.randint(9,size=(3,2,2))
for i in range(a.shape[0]):
m = a[i]
print(type(m))
print(m)
<class 'numpy.ndarray'>
[[5 0]
[3 3]]
<class 'numpy.ndarray'>
[[7 3]
[5 2]]
<class 'numpy.ndarray'>
[[4 7]
[6 8]]
Now if I want to loop through each of the matrices I get an error:
for i in range(a.shape[0]):
m = a[i]
for j in range(m):
print(j)
TypeError: only integer scalar arrays can be converted to a scalar index
Why is that ? Can't really debug this typerror.
rangecan't take anumpy.ndarrayas an argument. What are you trying to do withrange(m)? Do you meanfor j in m?