Uncle Iroh
Uncle Iroh

Reputation: 31

Numpy Matrix Indexing Equivalence

I have a matrix mat:

 array([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10],
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20],
        [21, 22, 23, 24, 25]])

I am wondering why mat[:3,1:2] is:

 array([[ 2],
        [ 7],
        [12]])`

But mat[:3,1] is:

 array([ 2, 7, 12])

Upvotes: 2

Views: 47

Answers (1)

Uncle Iroh
Uncle Iroh

Reputation: 31

An integer, i, returns the same values as i:i+1 except the dimensionality of the returned object is reduced by 1. In particular, a selection tuple with the p-th element an integer (and all other entries :) returns the corresponding sub-array with dimension N - 1. If N = 1 then the returned object is an array scalar. These objects are explained in Scalars. https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html

Thank you @Kasramvd

Upvotes: 1

Related Questions