I have the indices of a 2D array. I want to partition the indices such that the corresponding entries form blocks (block size is given as input m and n). I want to track the indices of the blocks too.
For example, if the indices are as given below
(array([0, 0, 0, 0, 1, 1, 1, 1, 6, 6, 6, 6, 7, 7, 7, 7 ]), array([0, 1, 7, 8, 0,1,7,8, 0,1,7,8, 0, 1, 7, 8]))
for the original matrix (from which the indices are generated)
array([[3, 4, 2, 0, 1, 1, 0, 2, 4],
[1, 3, 2, 0, 0, 1, 0, 4, 0],
[1, 0, 0, 1, 1, 0, 1, 1, 3],
[0, 0, 0, 3, 3, 0, 4, 0, 4],
[4, 3, 4, 2, 1, 1, 0, 0, 4],
[0, 1, 0, 4, 4, 2, 2, 2, 1],
[2, 4, 0, 1, 1, 0, 0, 2, 1],
[0, 4, 1, 3, 3, 2, 3, 2, 4]])
and if the block size is (2,2), then the blocks should be
[[3, 4],
[1, 3]]
[[2, 4]
[4, 0]]
[[2, 4]
[0, 4]]
[[2, 1]
[2, 4]]
I tried with reshape as A[inds].reshape(4,2,2). But it is not working. I even tried to transpose the axis with no success. Also, I am not sure how can i track the indices in each block.
*** The below code is not working in the general case.
For the below array
array([[(1., 1.), (1., 2.), (1., 3.), (1., 4.), (1., 5.), (1., 6.),
(1., 7.), (1., 8.)],
[(2., 1.), (2., 2.), (2., 3.), (2., 4.), (2., 5.), (2., 6.),
(2., 7.), (2., 8.)],
[(3., 1.), (3., 2.), (3., 3.), (3., 4.), (3., 5.), (3., 6.),
(3., 7.), (3., 8.)],
[(4., 1.), (4., 2.), (4., 3.), (4., 4.), (4., 5.), (4., 6.),
(4., 7.), (4., 8.)],
[(5., 1.), (5., 2.), (5., 3.), (5., 4.), (5., 5.), (5., 6.),
(5., 7.), (5., 8.)],
[(6., 1.), (6., 2.), (6., 3.), (6., 4.), (6., 5.), (6., 6.),
(6., 7.), (6., 8.)],
[(7., 1.), (7., 2.), (7., 3.), (7., 4.), (7., 5.), (7., 6.),
(7., 7.), (7., 8.)],
[(8., 1.), (8., 2.), (8., 3.), (8., 4.), (8., 5.), (8., 6.),
(8., 7.), (8., 8.)]], dtype=[('f0', '<f2'), ('f1', '<f2')])
with indices
(array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7]), array([0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5,
6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3,
4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7]))
and with a block size of (4,4) it returns the below result
array([[[(1., 1.), (1., 2.), (1., 3.), (1., 4.)],
[(2., 1.), (2., 2.), (2., 3.), (2., 4.)],
[(1., 5.), (1., 6.), (1., 7.), (1., 8.)],
[(2., 5.), (2., 6.), (2., 7.), (2., 8.)]],
[[(3., 1.), (3., 2.), (3., 3.), (3., 4.)],
[(3., 5.), (3., 6.), (3., 7.), (3., 8.)],
[(4., 1.), (4., 2.), (4., 3.), (4., 4.)],
[(4., 5.), (4., 6.), (4., 7.), (4., 8.)]],
[[(5., 1.), (5., 2.), (5., 3.), (5., 4.)],
[(5., 5.), (5., 6.), (5., 7.), (5., 8.)],
[(6., 1.), (6., 2.), (6., 3.), (6., 4.)],
[(6., 5.), (6., 6.), (6., 7.), (6., 8.)]],
[[(7., 1.), (7., 2.), (7., 3.), (7., 4.)],
[(7., 5.), (7., 6.), (7., 7.), (7., 8.)],
[(8., 1.), (8., 2.), (8., 3.), (8., 4.)],
[(8., 5.), (8., 6.), (8., 7.), (8., 8.)]]],
dtype=[('f0', '<f2'), ('f1', '<f2')])
The correct result should be
array([[[(1., 1.), (1., 2.), (1., 3.), (1., 4.)],
[(2., 1.), (2., 2.), (2., 3.), (2., 4.)],
[(3., 1.), (3., 2.), (3., 3.), (3., 4.)],
[(4., 1.), (4., 2.), (4., 3.), (4., 4.)]],
[[(1., 5.), (1., 6.), (1., 7.), (1., 8.)],
[(2., 5.), (2., 6.), (2., 7.), (2., 8.)]
[(3., 5.), (3., 6.), (3., 7.), (3., 8.)],
[(4., 5.), (4., 6.), (4., 7.), (4., 8.)]],
[[(5., 1.), (5., 2.), (5., 3.), (5., 4.)],
[(6., 1.), (6., 2.), (6., 3.), (6., 4.)],
[(7., 1.), (7., 2.), (7., 3.), (7., 4.)],
[(8., 1.), (8., 2.), (8., 3.), (8., 4.)]],
[[(5., 5.), (5., 6.), (5., 7.), (5., 8.)],
[(6., 5.), (6., 6.), (6., 7.), (6., 8.)],
[(7., 5.), (7., 6.), (7., 7.), (7., 8.)],
[(8., 5.), (8., 6.), (8., 7.), (8., 8.)]]],
dtype=[('f0', '<f2'), ('f1', '<f2')])