Home > Software engineering >  Numpy delete submatrix
Numpy delete submatrix

Time:02-03

I have a numpy array of float. I was wondering if there are any methods or functions to cut/delete a part of matrix.

matrix = np.arange(400).reshape(20,20)

I considered extracting by indexing the rows/columns by slicing (Cut(matrix[row_idx,:][:,col_idx])) or using functions np.ix, but that gives me a submatrix, and I need an old matrix without a select part.

The picture below shows what I mean. I want to get a matrix without the column and rows marked in yellow.

enter image description here

Is there any way to do this? Thank you for your any help.

CodePudding user response:

You can use np.delete() to delete the columns/rows that you want. This will return a new array

https://numpy.org/doc/stable/reference/generated/numpy.delete.html

CodePudding user response:

If you want to build a new matrix, you could use concatenate:

newmatrix = np.concatenate(
    (np.concatenate((matrix[:8,:8], matrix[13:, :8])),
     np.concatenate((matrix[:8,13:], matrix[13:, 13:]))),
    axis=1)
  •  Tags:  
  • Related