Numpy operation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Numpy operation

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

29th Nov 2018, 12:11 PM
Bishu Giri
Bishu Giri - avatar
2 Answers
+ 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
29th Nov 2018, 1:55 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
4
2nd Dec 2018, 1:42 PM
s.balajiselvam