Numpy extract submatrix

Posted on

Question :

Numpy extract submatrix

I’m pretty new in numpy and I am having a hard time understanding how to extract from a np.array a sub matrix with defined columns and rows:

Y = np.arange(16).reshape(4,4)

If I want to extract columns/rows 0 and 3, I should have:

[[0 3]
 [12 15]]

I tried all the reshape functions…but cannot figure out how to do this. Any ideas?

Answer #1:

Give np.ix_ a try:


This returns your desired result:

In [25]: Y = np.arange(16).reshape(4,4)
In [26]: Y[np.ix_([0,3],[0,3])]
array([[ 0,  3],
       [12, 15]])
Answered By: JoshAdel

Answer #2:

One solution is to index the rows/columns by slicing/striding. Here’s an example where you are extracting every third column/row from the first to last columns (i.e. the first and fourth columns)

In [1]: import numpy as np
In [2]: Y = np.arange(16).reshape(4, 4)
In [3]: Y[0:4:3, 0:4:3]
Out[1]: array([[ 0,  3],
               [12, 15]])

This gives you the output you were looking for.

For more info, check out this page on indexing in NumPy.

Answered By: mdml

Answer #3:

print y[0:4:3,0:4:3]

is the shortest and most appropriate fix .

Answered By: Hima

Answer #4:

First of all, your Y only has 4 col and rows, so there is no col4 or row4, at most col3 or row3.

To get 0, 3 cols: Y[[0,3],:]
To get 0, 3 rows: Y[:,[0,3]]

So to get the array you request: Y[[0,3],:][:,[0,3]]

Note that if you just Y[[0,3],[0,3]] it is equivalent to [Y[0,0], Y[3,3]] and the result will be of two elements: array([ 0, 15])

Answered By: CT Zhu

Answer #5:

You can also do this using:


which is equivalent to doing this using indexing arrays:

idx = np.array((0,3)).reshape(2,1)

To make the broadcasting work as desired, you need the non-singleton dimension of your indexing array to be aligned with the axis you’re indexing into, e.g. for an n x m 2D subarray:

Y[<n x 1 array>,<1 x m array>]

This doesn’t create an intermediate array, unlike CT Zhu’s answer, which creates the intermediate array Y[(0,3),:], then indexes into it.

Answered By: ali_m

Answer #6:

This can also be done by slicing: Y[[0,3],:][:,[0,3]]. More elegantly, it is possible to slice arrays (or even reorder them) by given sets of indices for rows, columns, pages, etcetera:

print(Y[r,:][:,c]) #>>[[ 0  3][12 15]]

for reordering try this:

print(Y[r,:][:,c])#>>[[ 3  0][15 12]]
Answered By: Father Geppeto

Leave a Reply

Your email address will not be published. Required fields are marked *