# Numpy: How to extract a Submatrix from an array

Last updated: Apr 12, 2024
4 min

## #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
```Copied!```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)
``````

Running the code sample produces the following output.

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

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

main.py
```Copied!```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
```Copied!```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 following example extracts the second and fourth elements of the second row and the second and fourth elements of the fourth row.

main.py
```Copied!```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
```Copied!```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
```Copied!```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)
``````

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
```Copied!```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)
``````

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

main.py
```Copied!```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)
``````

Notice that indexing is zero-based.

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