Look at the shape after indexing:

CopyIn [295]: A=np.matrix([1,2,3])
In [296]: A.shape
Out[296]: (1, 3)
In [297]: A[0]
Out[297]: matrix([[1, 2, 3]])
In [298]: A[0].shape
Out[298]: (1, 3)

The key to this behavior is that np.matrix is always 2d. So even if you select one row (A[0,:]), the result is still 2d, shape (1,3). So you can string along as many [0] as you like, and nothing new happens.

What are you trying to accomplish with A[0][0]? The same as A[0,0]? For the base np.ndarray class these are equivalent.

Note that Python interpreter translates indexing to __getitem__ calls.

Copy A.__getitem__(0).__getitem__(0)
 A.__getitem__((0,0))

[0][0] is 2 indexing operations, not one. So the effect of the second [0] depends on what the first produces.

For an array A[0,0] is equivalent to A[0,:][0]. But for a matrix, you need to do:

CopyIn [299]: A[0,:][:,0]
Out[299]: matrix([[1]])  # still 2d

=============================

"An array of itself", but I doubt anyone in their right mind would choose that as a model for matrices in a scientific library.

What is, then, the logic to the output I obtained? Why would the first element of a matrix object be itself?

In addition, A[0,:] is not the same as A[0]

In light of these comments let me suggest some clarifications.

A[0] does not mean 'return the 1st element'. It means select along the 1st axis. For a 1d array that means the 1st item. For a 2d array it means the 1st row. For ndarray that would be a 1d array, but for a matrix it is another matrix. So for a 2d array or matrix, A[i,:] is the same thing as A[i].

A[0] does not just return itself. It returns a new matrix. Different id:

CopyIn [303]: id(A)
Out[303]: 2994367932
In [304]: id(A[0])
Out[304]: 2994532108

It may have the same data, shape and strides, but it's a new object. It's just as unique as the ith row of a many row matrix.

Most of the unique matrix activity is defined in: numpy/matrixlib/defmatrix.py. I was going to suggest looking at the matrix.__getitem__ method, but most of the action is performed in np.ndarray.__getitem__.

np.matrix class was added to numpy as a convenience for old-school MATLAB programmers. numpy arrays can have almost any number of dimensions, 0, 1, .... MATLAB allowed only 2, though a release around 2000 generalized it to 2 or more.

Answer from hpaulj on Stack Overflow
Top answer
1 of 2
8

Look at the shape after indexing:

CopyIn [295]: A=np.matrix([1,2,3])
In [296]: A.shape
Out[296]: (1, 3)
In [297]: A[0]
Out[297]: matrix([[1, 2, 3]])
In [298]: A[0].shape
Out[298]: (1, 3)

The key to this behavior is that np.matrix is always 2d. So even if you select one row (A[0,:]), the result is still 2d, shape (1,3). So you can string along as many [0] as you like, and nothing new happens.

What are you trying to accomplish with A[0][0]? The same as A[0,0]? For the base np.ndarray class these are equivalent.

Note that Python interpreter translates indexing to __getitem__ calls.

Copy A.__getitem__(0).__getitem__(0)
 A.__getitem__((0,0))

[0][0] is 2 indexing operations, not one. So the effect of the second [0] depends on what the first produces.

For an array A[0,0] is equivalent to A[0,:][0]. But for a matrix, you need to do:

CopyIn [299]: A[0,:][:,0]
Out[299]: matrix([[1]])  # still 2d

=============================

"An array of itself", but I doubt anyone in their right mind would choose that as a model for matrices in a scientific library.

What is, then, the logic to the output I obtained? Why would the first element of a matrix object be itself?

In addition, A[0,:] is not the same as A[0]

In light of these comments let me suggest some clarifications.

A[0] does not mean 'return the 1st element'. It means select along the 1st axis. For a 1d array that means the 1st item. For a 2d array it means the 1st row. For ndarray that would be a 1d array, but for a matrix it is another matrix. So for a 2d array or matrix, A[i,:] is the same thing as A[i].

A[0] does not just return itself. It returns a new matrix. Different id:

CopyIn [303]: id(A)
Out[303]: 2994367932
In [304]: id(A[0])
Out[304]: 2994532108

It may have the same data, shape and strides, but it's a new object. It's just as unique as the ith row of a many row matrix.

Most of the unique matrix activity is defined in: numpy/matrixlib/defmatrix.py. I was going to suggest looking at the matrix.__getitem__ method, but most of the action is performed in np.ndarray.__getitem__.

np.matrix class was added to numpy as a convenience for old-school MATLAB programmers. numpy arrays can have almost any number of dimensions, 0, 1, .... MATLAB allowed only 2, though a release around 2000 generalized it to 2 or more.

2 of 2
1

Imagine you have the following

Copy>> A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) 

If you want to get the second column value, use the following:

Copy>> A.T[1]
array([ 2,  6, 10])
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.6.dev0 Manual
As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list.
๐ŸŒ
Medium
medium.com โ€บ @whyamit404 โ€บ basics-of-numpy-array-indexing-9052e6d6b5cf
Basics of NumPy Array Indexing. If you think you need to spend $2,000โ€ฆ | by whyamit404 | Medium
February 9, 2025 - In matrix[-1, -2], the -1 gets us the last row, and the -2 takes the second-to-last column. Itโ€™s like flipping the array on its head and grabbing items from the other end! Why does this matter? Understanding basic indexing is your ticket to mastering NumPy.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-indexing
Numpy Array Indexing - GeeksforGeeks
May 16, 2025 - A 1D NumPy array is a sequence of values with positions called indices which starts at 0. We access elements by using these indices in square brackets like arr[0] for the first element.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_indexing.asp
NumPy Array Indexing
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ numpy โ€บ python numpy matrix indexing
NumPy Matrix Indexing | Delft Stack
March 14, 2025 - Conversely, if you want to get an entire column, you can specify all rows while selecting a particular column, like matrix[:, 1], which returns the second column. This flexibility allows for powerful data manipulation. NumPy also offers advanced indexing techniques that can be incredibly useful for more complex operations.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ how-to-index.html
How to index ndarrays โ€” NumPy v2.6.dev0 Manual
Use Basic indexing features like Slicing and striding, and Dimensional indexing tools.
๐ŸŒ
All About AI-ML
indhumathychelliah.com โ€บ 2020 โ€บ 10 โ€บ 07 โ€บ how-to-index-data-in-python-numpy-arrays
How to Index Data in Python Numpy Arrays โ€“ All About AI-ML
January 2, 2022 - Indexing and slicing Photo by Magda Ehlers from Pexels Basic Indexing Basic Indexing can be done to access a particular element from the NumPy arrays. 1. One-dimensional array Creating a one-dimensional array import numpy as np one_d=np.array([10,20,30,40]) print (one_d) #Output:array([10, 20, 30, 40]) Image by Author Accessing elements from the array by positive indexing In Python, allโ€ฆ
Find elsewhere
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module3_IntroducingNumpy โ€บ BasicIndexing.html
Introducing Basic and Advanced Indexing โ€” Python Like You Mean It
Thus far we have seen that we can access the contents of a NumPy array by specifying an integer or slice-object as an index for each one of its dimensions. Indexing into and slicing along the dimensions of an array are known as basic indexing.
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ PythonDataScienceHandbook โ€บ 02.07-fancy-indexing.html
Fancy Indexing | Python Data Science Handbook
The at() method does an in-place application of the given operator at the specified indices (here, i) with the specified value (here, 1). Another method that is similar in spirit is the reduceat() method of ufuncs, which you can read about in the NumPy documentation.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.4 Manual
As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ matrix
Python Matrix and Introduction to NumPy
As you can see, NumPy made our task much easier. ... Similar like lists, we can access matrix elements using index.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.4 Manual
Another difference between an array ... the index along each axis within a single set of square brackets, separated by commas. For instance, the element 8 is in row 1 and column 3: ... It is familiar practice in mathematics to refer to elements of a matrix by the row index ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.21 โ€บ reference โ€บ arrays.indexing.html
Indexing โ€” NumPy v1.21 Manual
June 22, 2021 - As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python numpy array indexing
Python NumPy Array Indexing - Spark By {Examples}
March 27, 2024 - Python NumPy array indexing is used to access values in the 1-dimensional and, multi-dimensional arrays. Indexing is an operation, that uses this feature to get a selected set of values from a NumPy array.
๐ŸŒ
Readthedocs
scipy-cookbook.readthedocs.io โ€บ items โ€บ Indexing.html
Indexing numpy arrays โ€” SciPy Cookbook documentation
June 1, 2008 - It happens with some frequency that one wants to pull out values at a particular location in an array. If one wants a single location, one can just use simple indexing. But if there are many locations, you need something a bit more clever. Fortunately numpy supports a mode of fancy indexing that accomplishes this:
๐ŸŒ
Quansight-labs
quansight-labs.github.io โ€บ ndindex โ€บ indexing-guide โ€บ index.html
Guide to NumPy Indexing - ndindex documentation
This section is itself split into six subsections. First is a basic introduction to what a NumPy array is. Following this are pages for each of the remaining index types, the basic indices: tuples, ellipses, and newaxis; and the advanced indices: integer arrays and boolean arrays (i.e., masks).