Using a numpy array to assign values to another array
<p>I have the following numpy array <code>matrix</code> ,</p>

<pre><code>matrix = np.zeros((3,5), dtype = int)

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

<p>Suppose I have this numpy array <code>indices</code> as well</p>

<pre><code>indices = np.array([[1,3], [2,4], [0,4]])

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

<p><strong>Question:</strong> How can I assign <code>1</code>s to the elements in the <code>matrix</code> where their indices are specified by the <code>indices</code> array. A vectorized implementation is expected.</p>

<p>For more clarity, the output should look like:</p>

<pre><code>    array([[0, 1, 0, 1, 0], #[1,3] elements are changed
           [0, 0, 1, 0, 1], #[2,4] elements are changed
           [1, 0, 0, 0, 1]]) #[0,4] elements are changed
</code></pre>

<p>Here's one approach using <a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing" rel="nofollow"><code>NumPy's fancy-indexing</code></a> -</p>

<pre><code>matrix[np.arange(matrix.shape[0])[:,None],indices] = 1
</code></pre>

<p><strong>Explanation</strong></p>

<p>We create the row indices with <code>np.arange(matrix.shape[0])</code> -</p>

<pre><code>In [16]: idx = np.arange(matrix.shape[0])

In [17]: idx
Out[17]: array([0, 1, 2])

In [18]: idx.shape
Out[18]: (3,)
</code></pre>

<p>The column indices are already given as <code>indices</code> -</p>

<pre><code>In [19]: indices
Out[19]: 
array([[1, 3],
       [2, 4],
       [0, 4]])

In [20]: indices.shape
Out[20]: (3, 2)
</code></pre>

<p>Let's make a schematic diagram of the shapes of row and column indices, <code>idx</code> and <code>indices</code> -</p>

<pre><code>idx     (row) :      3 
indices (col) :  3 x 2
</code></pre>

<p>For using the row and column indices for indexing into input array <code>matrix</code>, we need to make them broadcastable against each other. One way would be to introduce a new axis into <code>idx</code>, making it <code>2D</code> by pushing the elements into the first axis and allowing a singleton dim as the last axis with <code>idx[:,None]</code>, as shown below -</p>

<pre><code>idx     (row) :  3 x 1
indices (col) :  3 x 2
</code></pre>

<p>Internally, <code>idx</code> would be broadcasted, like so -</p>

<pre><code>In [22]: idx[:,None]
Out[22]: 
array([[0],
       [1],
       [2]])

In [23]: indices
Out[23]: 
array([[1, 3],
       [2, 4],
       [0, 4]])

In [24]: np.repeat(idx[:,None],2,axis=1) # indices has length of 2 along cols
Out[24]: 
array([[0, 0],  # Internally broadcasting would be like this
       [1, 1],
       [2, 2]]) 
</code></pre>

<p>Thus, the broadcasted elements from <code>idx</code> would be used as row indices and column indices from <code>indices</code> for indexing into <code>matrix</code> for setting elements in it. Since, we had -</p>

<p><code>idx = np.arange(matrix.shape[0])</code>, </p>

<p>Thus, we would end up with -</p>

<p><code>matrix[np.arange(matrix.shape[0])[:,None],indices]</code> for setting elements.</p>

python arrays numpy indexing vectorization