Suppose I have a 27×27 ndarray
of integers A that I want to reduce to a 25-length vector b such that it contains the following values (in some order):
numpy.sum(A[0:9, 0:9])
numpy.sum(A[0:9, 9:18])
numpy.sum(A[0:9, 18:27])
numpy.sum(A[9:18, 0:9])
numpy.sum(A[9:18, 18:27])
numpy.sum(A[18:27, 0:9])
numpy.sum(A[18:27, 9:18])
numpy.sum(A[18:27, 18:27])
numpy.sum(A[9:12, 9:12])
numpy.sum(A[9:12, 12:15])
numpy.sum(A[9:12, 15:18])
numpy.sum(A[12:15, 9:12])
numpy.sum(A[12:15, 15:18])
numpy.sum(A[15:18, 9:12])
numpy.sum(A[15:18, 12:15])
numpy.sum(A[15:18, 15:18])
numpy.sum(A[12:13, 12:13])
numpy.sum(A[12:13, 13:14])
numpy.sum(A[12:13, 14:15])
numpy.sum(A[13:14, 12:13])
numpy.sum(A[13:14, 14:15])
numpy.sum(A[14:15, 12:13])
numpy.sum(A[14:15, 13:14])
numpy.sum(A[14:15, 14:15])
numpy.sum(A[13:14, 13:14])
In other words, each colored square in the below matrix A should have its elements summed. Each sum should be an element in vector b.
Coloring image
I want to convert a given A into b very efficiently, so I want to do this via numpy functions, not by manually computing the sum of each square block and not by doing extra arithmetic operations (I know I can flatten the A into a vector and convert it to b via a matrix multiplication, but that requires more multiplications).
Thank you!
Sanjeev Rajakumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.