これは、超大規模な配列にはうまくスケールアップできず、それほど堅牢ではありませんが、とにかくかわいらしいものです。
テスト配列を作成します。
In [148]: import numpy as np
In [149]: m = np.random.random((8, 9))
In [150]: m
Out[150]:
array([[ 0.36254361, 0.92449435, 0.36954906, 0.13007562, 0.95031795,
0.1341706 , 0.6417435 , 0.25467616, 0.72431605],
[ 0.75959927, 0.53992222, 0.76544104, 0.94409118, 0.27048638,
0.44747388, 0.42691671, 0.75695594, 0.10366086],
[ 0.12424304, 0.06642197, 0.08953764, 0.66546555, 0.30932551,
0.50375697, 0.1344662 , 0.31008366, 0.13953257],
[ 0.28643294, 0.12986936, 0.39022482, 0.72869735, 0.84537494,
0.62683481, 0.88539889, 0.35697751, 0.96332492],
[ 0.67678959, 0.60373389, 0.15151052, 0.53586538, 0.50470088,
0.39664842, 0.93584004, 0.97386657, 0.50405521],
[ 0.04613492, 0.53676995, 0.60119919, 0.5559467 , 0.09392262,
0.69938864, 0.35719754, 0.79775878, 0.16634076],
[ 0.00879703, 0.60874483, 0.25390384, 0.48368248, 0.52770161,
0.64563258, 0.27353424, 0.86046696, 0.04489414],
[ 0.80883157, 0.44484305, 0.52325907, 0.49172028, 0.46731106,
0.06400542, 0.75671515, 0.55930335, 0.70721442]])
インデックスを取得します。
In [151]: centre = (3,5)
In [152]: ii = np.indices(m.shape)
In [153]: ii
Out[153]:
array([[[0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5],
[6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7]],
[[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[0, 1, 2, 3, 4, 5, 6, 7, 8]]])
In [154]: di = ii[0]-centre[0], ii[1]-centre[1]
二乗距離を取得します。
In [155]: dr2 = (di[0]**2+di[1]**2)
In [156]: dr2
Out[156]:
array([[34, 25, 18, 13, 10, 9, 10, 13, 18],
[29, 20, 13, 8, 5, 4, 5, 8, 13],
[26, 17, 10, 5, 2, 1, 2, 5, 10],
[25, 16, 9, 4, 1, 0, 1, 4, 9],
[26, 17, 10, 5, 2, 1, 2, 5, 10],
[29, 20, 13, 8, 5, 4, 5, 8, 13],
[34, 25, 18, 13, 10, 9, 10, 13, 18],
[41, 32, 25, 20, 17, 16, 17, 20, 25]])
bincount
合計を取得するために使用します。
In [158]: np.bincount(dr2.flat, m.flat)
Out[158]:
array([ 0.62683481, 2.63117922, 1.88433263, 0. , 2.23253738,
3.63380441, 0. , 0. , 3.05475259, 2.13335292,
3.27793323, 0. , 0. , 3.36554308, 0. ,
0. , 0.19387479, 1.89418207, 1.3926631 , 0. ,
2.12771579, 0. , 0. , 0. , 0. ,
3.05014562, 0.80103263, 0. , 0. , 0.8057342 ,
0. , 0. , 0.44484305, 0. , 0.37134064,
0. , 0. , 0. , 0. , 0. ,
0. , 0.80883157])