Reputation: 612
Can any one tell how to interpret this kind of addressing : a[2::2, ::2]? I have no idea how to use this kind of indexing.
a = np.random.random((6,6))
print(a)
[[0.17948771 0.61436323 0.48768101 0.20540278 0.15331527 0.17766787]
[0.40028608 0.63915391 0.92719741 0.56604286 0.92959065 0.92707981]
[0.27554561 0.09253335 0.73841082 0.00840638 0.33683454 0.89065058]
[0.25030938 0.37093169 0.70789081 0.95205777 0.60483874 0.81425781]
[0.14250593 0.56916738 0.45440191 0.21140548 0.72945015 0.59313599]
[0.68116512 0.45349473 0.23057609 0.36349226 0.622701 0.07951557]]
a[2::2, ::2]
array([[0.27554561, 0.73841082, 0.33683454],
[0.14250593, 0.45440191, 0.72945015]])
Upvotes: 1
Views: 680
Reputation: 2948
a[2:]
will return a slice of a
from index 2
to the end of the list. a[2::2]
will do the same, but will only return every second value, a[2::3]
every third etc.. So if
a = [0,1,2,3,4]
then a[2::2]
will be [2,4]
(2 to 4, but only every second). For numpy arrays a[2:,:]
will return a slice of a
starting from index 2
of the first dimension and the entirety of the second dimension. a[2::2, ::2]
will thus return only every second element compared to a[2:,:]
.
In your example, the returned elements are from the following indices
[[(2,0), (2,2), (2,4)], [(4,0), (4,2), (4,4)]]
Upvotes: 6