A more compact way is new = m[0, :12:2, :12:2] . This is what numpy docs are called โbasic indexing,โ which means you slice an integer or shear object (i.e. 0: 12: 2). When using basic indexing, numpy returns a representation of the original array. For instance:
In [3]: a = np.zeros((2, 3, 4)) In [4]: b = a[0, 1, ::2] In [5]: b Out[5]: array([ 0., 0.]) In [6]: b[:] = 7 In [7]: a Out[7]: array([[[ 0., 0., 0., 0.], [ 7., 0., 7., 0.], [ 0., 0., 0., 0.]], [[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]]])
In your "intuitive" approach, what you are doing is indexing an array with another array. When you index a numpy array with another array, the arrays must be the same size (or they must be broadcast against each other, moreover, per second). In documents, this is called fancy indexing or advanced indexing. For instance:
In [10]: a = np.arange(9).reshape(3,3) In [11]: a Out[11]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [12]: index = np.array([0,1,2]) In [13]: b = a[index, index] In [14]: b Out[14]: array([0, 4, 8])
You see that I get [0,0], [1,1] and [2,2] not a [0,0], a [0,1] ... If you want the "external product" of the index with an index you can do the following.
In [22]: index1 = np.array([[0,0],[1,1]]) In [23]: index2 = np.array([[0,1],[0,1]]) In [24]: b = a[index1, index2] In [25]: b Out[25]: array([[0, 1], [3, 4]])
There is a summary for this:
In [28]: index = np.array([0,1]) In [29]: index1, index2 = np.ix_(index, index) In [31]: index1 Out[31]: array([[0], [1]]) In [32]: index2 Out[32]: array([[0, 1]]) In [33]: a[index1, index2] Out[33]: array([[0, 1], [3, 4]]) In [34]: a[np.ix_(index, index)] Out[34]: array([[0, 1], [3, 4]])
You will notice that index1 is (2, 1) and index2 is (1, 2) , not (2, 2) . This is because the two arrays are passed to each other, you can learn more about the broadcast here . Keep in mind that when you use fantastic indexing, you get a copy of the source data, not a view. Sometimes itโs better (if you want to leave the original data unchanged), and sometimes it just takes more memory. Read more about indexing here .