# How to extract out a non-continuous matrix from a larger matrix based on a list [closed]

Solution for How to extract out a non-continuous matrix from a larger matrix based on a list [closed]
is Given Below:

(this is in python 2.7.13)

If I have matrix A:

``````a =[
[0,1,0,0,0,1],
[4,1,0,3,2,0],
[0,0,1,0,0,0],
[0,1,0,0,1,0],
[0,0,0,0,1,0],
[0,0,0,0,0,0],
]
``````

and a list of index’s `b =[0, 1, 3]`

I would like to extract a smaller matrix from the `matrix a` using the index `list b` as the rows and columns.

So, it would be:

``````[0] row of matrix a; columns[0,1,3]
[1] row of matrix a; columns[0,1,3]
[3] row of matrix a; columns[0,1,3]
``````

Which would equal, as example:

``````[
[0,1,0], #0 row, 0,1,3 items
[4,1,3], #1 row, 0,1,3 items
[0,1,0]  #3 row, 0,1,3 items
]
``````

You can do it by:

``````a =[
[0,1,0,0,0,1],
[4,1,0,3,2,0],
[0,0,1,0,0,0],
[0,1,0,0,1,0],
[0,0,0,0,1,0],
[0,0,0,0,0,0],
]

b = [0, 1, 3]
c = [[a[i][j] for j in b] for i in b]
print(c)
# ===>
# [[0, 1, 0], [4, 1, 3], [0, 1, 0]]
``````

(Anyway, Python2 is no longer supported: link. I recommend you use Python3+NumPy, which supports various matrix operations such as flexible indexing that you want.)