Let's say I have a 2D array of that represents the signal strength of a network, like so:
d = np.array([[ 0, 0, 0, 3],
[ 0, 2, 0, 3],
[ 0, 0, 0, 0],
[ 0, 0, 0, 3]])
I know the signal strength of specific transmission nodes in the network, and the ones I don't know will default to 0. The rules for the network strength is that for every node travelled by the signal, the signal strength decays by 1, and I should get
d = np.array([[ 0, 1, 2, 3],
[ 1, 2, 2, 3],
[ 0, 1, 1, 2],
[ 0, 1, 2, 3]])
as a result. What would be the best way to obtain the 2nd array from the first?
CodePudding user response:
I suspect you're not interpolating, but instead applying a max of the effects from each station. Extend to a three dimensional matrix where the added dimension is the number of stations, and then do a max over that dimension. The following produces the same output (made non-square to avoid transposition errors):
import numpy as np
input_stations = np.array((
( 0, 0, 0, 3),
( 0, 2, 0, 3),
( 0, 0, 0, 0),
( 0, 0, 0, 3),
( 0, 0, 0, 0),
))
coords = input_stations.nonzero()
stations = input_stations[coords]
station_y, station_x = input_stations.nonzero()
m, n = input_stations.shape
# stations by y by x
station_strengths = np.clip(
stations[:, np.newaxis, np.newaxis]
- np.abs(
np.arange(m)[np.newaxis, :, np.newaxis] - station_y[:, np.newaxis, np.newaxis]
)
- np.abs(
np.arange(n)[np.newaxis, np.newaxis, :] - station_x[:, np.newaxis, np.newaxis]
),
a_min=0, a_max=None,
)
strengths = station_strengths.max(axis=0)
expected = np.array((
( 0, 1, 2, 3),
( 1, 2, 2, 3),
( 0, 1, 1, 2),
( 0, 1, 2, 3),
( 0, 0, 1, 2),
))
assert np.all(expected == strengths)