python 2.7 - Extratcing all square submatrices from matrix using Numpy -


say have nxn numpy matrix. looking fastest way of extracting square chunks (sub-matrices) matrix. meaning cxc parts of original matrix 0 < c < n+1. sub-matrices should correspond contiguous rows/columns indexes of original matrix. want achieve in little time possible.

you use numpy slicing,

import numpy np  n = 20 x = np.random.rand(n, n)  slice_list = [slice(k, l) k in range(0, n) l in range(k, n)]  results = [x[sl,sl] sl in slice_list] 

avoiding loops in numpy, not goal itself. long being mindful it, there shouldn't overhead.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -