回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个numpy数组:</p>
<pre><code>a = np.array([[0,4,3,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,3,5,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,8,3,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,2,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,2,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,9,2,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,2,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,2,2,6,8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,4,3,4,4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10],
[4,4,4,3,4,4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]])
</code></pre>
<p>我正在寻找单元格中最小的<a href="https://en.wikipedia.org/wiki/Von_Neumann_neighborhood" rel="nofollow noreferrer">neighbours</a>的索引,其中<code>value= 2</code>(<code>numpy.where(a==value)</code>)但大于<code>value</code>。我还需要相应单元格的索引=<code>value</code>,我们为其找到了最小的邻居。你知道吗</p>
<p>在这种情况下(<code>value = 2</code>)的结果应该是:</p>
<ul>
<li><p>邻近细胞指数:<code>[0,2]</code>和<code>[4,3]</code>
相应的和</p></li>
<li><p>相应细胞的索引:<code>[1,2]</code>和<code>[3,3]</code></p></li>
</ul>
<p>如果问题不太清楚,请道歉。你知道吗</p>
<p>到目前为止,我的情况是:</p>
<pre><code>import numpy as np
value = 2
neighbors = np.zeros(4, dtype = np.float)
fdx = np.flatnonzero(a== value)
locations = fdx // a.shape[1], fdx % a.shape[1]
maximums = []
for item in zip(*locations):
i, j = item[0], item[1]
neighbors[0], neighbors[1], neighbors[2],neighbors[3] = [a[i-1,j], a[i+1,j], a[i,j-1], a[i,j+1]]
maximums.append(min(neighbors[neighbors> value]))
print np.where(a==min(maximums))
prints: (array([0, 4]), array([2, 3]))
</code></pre>
<p>这是非常缓慢的,也仍然不知道如何找到相应的细胞指数。任何与我的解决方案完全不同的解决方案也将被接受。你知道吗</p>