Home > Software engineering >  unable to slice numpy array input
unable to slice numpy array input

Time:03-31

Expected output: [[1,0][1,0]] Expected output: [[0,0][0,1]] etc as the loops runs the range. Error: at print statement. The loop is unable to run through the different indexes. Error message only integer scalar arrays can be converted to a scalar index

import numpy as np
field = [[1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
    [1, 0, 1, 0, 0, 0, 0, 0, 1, 0],
    [1, 0, 1, 0, 1, 1, 1, 0, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
 a2= np.array(field)
 print(a2)
 for i in list(range(10)):
      for i in a2[i:i 2,i:i 2]:
         print(a2[i:i 2,i:i 2])

CodePudding user response:

If your goal is to print out a 2 by 2 matrix each time iterating through a2 from left to right and from top to bottom with step 1, the for loops should be like:

for i in range(9):
    for j in range(9):
        print(a2[i:i 2, j:j 2])

The outputs are:

[[1 0]
 [1 0]]
[[0 0]
 [0 1]]
[[0 0]
 [1 0]]
[[0 0]
 [0 0]]
[[0 1]
 [0 0]]

etc.

CodePudding user response:

That inside for loop should be for j in a2[i:i 2,i:i 2]: for it to work. it seems like

for i in range(10): 
    print(a2[i:i 2,i:i 2])

gives the same result

CodePudding user response:

The error is saying that you have used i once then it is not an array to iterate through indexes. Why use two for loops? One for can do the job. Again no need to cast list on range.

for i in range(10):
    print(a2[i:i 2,i:i 2])

CodePudding user response:

You could iterate the matrix using a 2x2 window by using the shape of the matrix as a reference and a for loop to slide each dimension (x and y). This is a possible implementation:

for i in range(2, a2.shape[0]):
    for j in range(2, a2.shape[1]):
        print(a2[i-2:i,j-2:j])

CodePudding user response:

Examine the values during the loops. This is basic python looping logic

In [310]: for i in range(2):        # don't need list(range...)
     ...:     print(i)
     ...:     print(a2[i:i 2, i:i 2])
     ...:     for i in a2[i:i 2, i:i 2]:
     ...:         print(i)
     ...: 
     ...: 
0              # outer i
[[1 0]         # the a2 window
 [1 0]]
[1 0]          # inner i, rows of that window
[1 0]
1              # outer i
[[0 1]
 [0 1]]
[0 1]
[0 1]

It does not make sense to use the inner i as indices to a2. They are already arrays derived from a2. I don't know why beginners make this mistake

 for i in alist:
     alist[i] ...   # i is an element of the list, not an index

With i being np.array([1,0]), this:

a2[i:i 2,i:i 2]
# a2[np.array([1,0]):np.array([3,2]), ....)

produces your error.

You used the first i in range correctly; why the change in the second?

Using a different iteration variable in the 2nd loop, and range again, produces are more logical nested iteration:

In [313]: for i in range(2):
     ...:     print(i)
     ...:     for j in range(2):
     ...:         print(j)
     ...:         print(a2[i:i 2, j:j 2])
  • Related