block matrix and sums of blocks

Hi, Let us consider one kN x kM array. What is the fastest way to sum each k x k square block of A and to put all these results into a NxM array B? For instance: If A = [112233 112233 223311 223311] then B = [4 8 12 4 12 4] No sanity checks on the arrays shapes are requiered. Only speed matters ;) Xavier

On Sun, Feb 22, 2009 at 6:39 PM, Xavier Gnata <xavier.gnata@gmail.com>wrote:
Hi,
Let us consider one kN x kM array. What is the fastest way to sum each k x k square block of A and to put all these results into a NxM array B?
For instance: If A = [112233 112233 223311 223311] then B = [4 8 12 4 12 4]
No sanity checks on the arrays shapes are requiered. Only speed matters ;)
Xavier _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

On Sun, Feb 22, 2009 at 6:39 PM, Xavier Gnata <xavier.gnata@gmail.com>wrote:
Hi,
Let us consider one kN x kM array. What is the fastest way to sum each k x k square block of A and to put all these results into a NxM array B?
For instance: If A = [112233 112233 223311 223311] then B = [4 8 12 4 12 4]
No sanity checks on the arrays shapes are requiered. Only speed matters ;)
An example with real numpy arrays would help ;) But basically you will need to reshape the matrix and sum along the last axis. Chuck

On Sun, Feb 22, 2009 at 19:39, Xavier Gnata <xavier.gnata@gmail.com> wrote:
Hi,
Let us consider one kN x kM array. What is the fastest way to sum each k x k square block of A and to put all these results into a NxM array B?
For instance: If A = [112233 112233 223311 223311] then B = [4 8 12 4 12 4]
No sanity checks on the arrays shapes are requiered. Only speed matters ;)
In [6]: A Out[6]: array([[1, 1, 2, 2, 3, 3], [1, 1, 2, 2, 3, 3], [2, 2, 3, 3, 1, 1], [2, 2, 3, 3, 1, 1]]) In [7]: k = 2 In [8]: add.reduceat(add.reduceat(A, arange(0, A.shape[0], k), axis=0), arange(0, A.shape[1], k), axis=1) Out[8]: array([[ 4, 8, 12], [ 8, 12, 4]]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

Robert Kern wrote:
On Sun, Feb 22, 2009 at 19:39, Xavier Gnata <xavier.gnata@gmail.com> wrote:
Hi,
Let us consider one kN x kM array. What is the fastest way to sum each k x k square block of A and to put all these results into a NxM array B?
For instance: If A = [112233 112233 223311 223311] then B = [4 8 12 4 12 4]
No sanity checks on the arrays shapes are requiered. Only speed matters ;)
In [6]: A Out[6]: array([[1, 1, 2, 2, 3, 3], [1, 1, 2, 2, 3, 3], [2, 2, 3, 3, 1, 1], [2, 2, 3, 3, 1, 1]])
In [7]: k = 2
In [8]: add.reduceat(add.reduceat(A, arange(0, A.shape[0], k), axis=0), arange(0, A.shape[1], k), axis=1) Out[8]: array([[ 4, 8, 12], [ 8, 12, 4]])
It does the job and I have learned .reduceat :) Thanks, Xavier
participants (3)
-
Charles R Harris
-
Robert Kern
-
Xavier Gnata