5

I am trying to delete every 8th column in a 1024x1024 matrix using this method but it takes a lot of time and it will be more difficult when I have to process millions of matrices. Could you please show me how can I delete every nth row or column starting from a certain row or column and using numpy, scipy or any other Python package?

Many thanks

Community
  • 1
  • 1
ticofiz
  • 107
  • 1
  • 1
  • 6

1 Answers1

5

You can use np.delete giving the indices corresponding to every 8th row index. Let a be a 2D array or a matrix:

np.delete(a, list(range(0, a.shape[0], 8)), axis=0)

note the use of axis=0 indicating to operate along the rows. To operate along the columns:

np.delete(a, list(range(0, a.shape[1], 8)), axis=1)
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234