Numpy: How to extract a Submatrix from an array

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Numpy: How to extract a Submatrix from an array

Use the numpy.ix_() method to extract a submatrix from an array.

The method returns N arrays with N dimensions each, where N is the number of input sequences.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(arr) print('-' * 50) submatrix = arr[np.ix_([0, 3], [1, 3])] # [[ 2 4] # [14 16]] print(submatrix)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] -------------------------------------------------- [[ 2 4] [14 16]]

extract submatrix from numpy array in python

The numpy.ix_ method constructs an open mesh from multiple sequences.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(arr) print('-' * 50) # (array([[0], [3]]), array([[1, 3]])) print(np.ix_([0, 3], [1, 3]))

The ix_() method returns a tuple of ndarrays.

Each array has N dimensions with N being the number of input sequences.

Together the arrays form an open mesh.

The arrays are then used to index the original array.

The following example extracts the first and fourth elements of the first row and the first and fourth elements of the last row.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(arr) print('-' * 50) submatrix = arr[np.ix_([0, 3], [0, 3])] # [[ 1 4] # [13 16]] print(submatrix)
The code for this article is available on GitHub

The following example extracts the second and fourth elements of the second row and the second and fourth elements of the fourth row.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(arr) print('-' * 50) submatrix = arr[np.ix_([1, 3], [1, 3])] # [[ 6 8] # [14 16]] print(submatrix)

The following example extracts the first and third elements of the first row and the first and third elements of the third row.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(arr) print('-' * 50) submatrix = arr[np.ix_([0, 2], [0, 2])] # [[ 1 3] # [ 9 11]] print(submatrix)

# Numpy: How to extract a Submatrix from an array using indexing

You can also use array indexing to extract a submatrix from a NumPy array.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) print(arr) print('-' * 50) submatrix = arr[0:4:3, 0:4:3] # [[ 1 4] # [13 16]] print(submatrix)

extract submatrix from numpy array using indexing

The code for this article is available on GitHub

The example selects every third column from the first to the last.

Note that array indices are zero-based, so the first element in an array has an index of 0 and the last has an index of len(array) - 1.

The following example selects every second column from the first to the last.

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]) print(arr) print('-' * 50) submatrix = arr[0:5:2, 0:5:2] # [[ 1 3] # [ 9 11] # [17 19]] print(submatrix)

select every second column from first to last

The following example selects rows 0, 1, 2 (0-2) and columns 1, 2, 3 (1-3).

main.py
import numpy as np arr = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], ]) print(arr) print('-' * 50) submatrix = arr[0:3, 1:4] # [[ 2 3 4] # [ 6 7 8] # [10 11 12]] print(submatrix)
The code for this article is available on GitHub

Notice that indexing is zero-based.

  • 0:3 means "select rows 0, 1 and 2".
  • 1:4 means "select columns 1, 2 and 3".

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.