I have a numpy array, and an array of indexs, how can I access to these positions at the same time
<p>for example, I have the numpy arrays like this</p>

<pre><code>a =
array([[1, 2, 3],
       [4, 3, 2]])
</code></pre>

<p>and index like this to select the max values</p>

<pre><code>max_idx = 
array([[0, 2],
       [1, 0]])
</code></pre>

<p>how can I access there positions at the same time, to modify them.
like "a[max_idx] = 0" getting the following</p>

<pre><code>array([[1, 2, 0],
       [0, 3, 2]])
</code></pre>

<p>Numpy support <a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#advanced-indexing" rel="nofollow">advanced slicing</a> like this:</p>

<pre><code>a[b[:, 0], b[:, 1]] = 0
</code></pre>

<p>Code above would fit your requirement. </p>

<p>If <code>b</code> is more than 2-D. A better way should be like this:</p>

<pre><code>a[np.split(b, 2, axis=1)]
</code></pre>

<p>The <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html" rel="nofollow">np.split</a> will split ndarray into columns.</p>

python arrays numpy