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>Simply use <code>subscripted-indexing</code> -</p>

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

<p>If you are working with higher dimensional arrays and don't want to type out slices of <code>max_idx</code> for each axis, you can use <code>linear-indexing</code> to assign <code>zeros</code>, like so -</p>

<pre><code>a.ravel()[np.ravel_multi_index(max_idx.T,a.shape)] = 0
</code></pre>

<p>Sample run -</p>

<pre><code>In [28]: a
Out[28]: 
array([[1, 2, 3],
       [4, 3, 2]])

In [29]: max_idx
Out[29]: 
array([[0, 2],
       [1, 0]])

In [30]: a[max_idx[:,0],max_idx[:,1]] = 0

In [31]: a
Out[31]: 
array([[1, 2, 0],
       [0, 3, 2]])
</code></pre>

python arrays numpy