Numpy operation | Sololearn: Learn to code for FREE!

+2

Numpy operation

Given a nxn matrix, how can we find all possible combination of 3x3 matrix?

11/29/2018 12:11:15 PM

Bishu Giri

2 Answers

New Answer

+3

I like to use numpy.ix_ to extract submatrices. import numpy as np from itertools import combinations n = 4 mat = np.arange(n**2).reshape((n, n)) print("Original matrix:\n", mat) print("\n3x3 submatrices:") for r in combinations(range(n), 3): for c in combinations(range(n), 3): print(mat[np.ix_(r, c)]) print() Reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ix_.html

0