Space-efficient way:
import numpy as np
hires = np.array((6, 12))
lowres = np.array((2,4))
h, w = hires // lowres
m = np.arange(np.prod(lowres)).reshape(lowres)
print(m)
# [[0 1 2 3]
# [4 5 6 7]]
v = [0, 1, 4, 24, 36, 42]
i, j = np.unravel_index(v, hires)
w = m[i // h, j // w]
print(w)
# [0 0 1 0 4 6]
Space-inefficient way:
import numpy as np
hires = np.array((6, 12))
lowres = np.array((2,4))
h, w = hires // lowres
# DON'T DO THIS. INEFFICIENT
m = np.kron(np.arange(np.prod(lowres)).reshape(lowres), np.ones(h, w), )
print(m)
# [[0. 0. 0. 1. 1. 1. 2. 2. 2. 3. 3. 3.]
# [0. 0. 0. 1. 1. 1. 2. 2. 2. 3. 3. 3.]
# [0. 0. 0. 1. 1. 1. 2. 2. 2. 3. 3. 3.]
# [4. 4. 4. 5. 5. 5. 6. 6. 6. 7. 7. 7.]
# [4. 4. 4. 5. 5. 5. 6. 6. 6. 7. 7. 7.]
# [4. 4. 4. 5. 5. 5. 6. 6. 6. 7. 7. 7.]]
v = [0, 1, 4, 24, 36, 42]
w = m[np.unravel_index(v, hires)]
print(w)
# [0. 0. 1. 0. 4. 6.]
The main idea here is to use np.unravel_index
to convert a "flat index" into a tuple of coordinates given the shape of the array you intend to index into.
For example,
In [446]: np.unravel_index([0, 1, 4, 24, 36, 42], (6, 12))
Out[446]: (array([0, 0, 0, 2, 3, 3]), array([0, 1, 4, 0, 0, 6]))
It returns two indexing arrays which together give the coordinates of the 0th, 1st, 4th, etc. "flattened" elements in an array of shape (6, 12).
The space-inefficient method constructs the big m
array and then finds w
by indexing m
with those coordinates: w = m[np.unravel_index(v, hires)]
.
The more space-efficent method simply integer-divides the coordinates by the block size (in this case, 3-by-3) to generate low-resolution coordinates.
This avoids the need to generate the big matrix m
. We can instead use a smaller matrix
In [447]: m = np.arange(np.prod(lowres)).reshape(lowres); m
Out[447]:
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
and index into that: w = m[i // h, j // w]
.
You might also be interested in np.ravel_multi_index
, which is the inverse of np.unravel_index
:
In [451]: np.ravel_multi_index((np.array([0, 0, 0, 2, 3, 3]), np.array([0, 1, 4, 0, 0, 6])), (6, 12))
Out[451]: array([ 0, 1, 4, 24, 36, 42])
It converts the coordinate arrays, i
and j
, back into v
.