0

for example,I have a matrix like this

   mat = np.diag((1,1,1,1,1,1))
        print(mat)
        out:[[1 0 0 0 0 0]
             [0 1 0 0 0 0]
             [0 0 1 0 0 0]
             [0 0 0 1 0 0]
             [0 0 0 0 1 0]
             [0 0 0 0 0 1]]

I may need some slices that can be combination of any lines and columns. if it is lines=[0,1,2] columns=[0,1,2],I could use:

mat[0:3,0:3]

If I need lines=[0,1,2,5] columns=[0,1,2,5],I write:

mat[[0,1,2,5],[0,1,2,5]]

I can only get:

out:[1 1 1 1]

But I wanna get a matrix of 4×4.By the way,the columns always equal lines.

family
  • 13
  • 2

1 Answers1

2

For non-contiguous indices you can do:

mat[[0,1,2,5],:][:,[0,1,2,5]]

i.e. first get the specified rows (gets a 4x6 matrix out of it) then get the specified columns from that.

fferri
  • 18,285
  • 5
  • 46
  • 95