Skip to content Skip to sidebar Skip to footer

How To Extract Out A Non-continuous Matrix From A Larger Matrix Based On A List

(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

Solution 1:

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.)

Post a Comment for "How To Extract Out A Non-continuous Matrix From A Larger Matrix Based On A List"