How to extract elements from a matrix using a vector of indices?
<p>Suppose I have a matrix <code>A</code> of order <code>m</code>×<code>n</code> and a vector of order <code>m</code>×<code>1</code>. I would like to extract elements from each row of the matrix <code>A</code> by using the elements of the vector as an offset in each row.</p>
<p>For example,</p>
<pre class="lang-py prettyprint-override"><code>A = [[3, 0, 0, 8, 3],
     [9, 3, 2, 2, 6],
     [5, 5, 4, 2, 8],
     [3, 8, 7, 1, 2],
     [3, 9, 1, 5, 5]]
</code></pre>
<p>and a vector</p>
<pre class="lang-py prettyprint-override"><code>y = [4, 2, 1, 3, 2]
</code></pre>
<p>What I want to achieve is a way to extract the elements of <code>A</code> such that each element of the vector indexes an element in the corresponding row of <code>A</code>, i.e., implementing</p>
<pre class="lang-py prettyprint-override"><code>for i in range(len(y)):
    A[i, y[i]] = #perform operations here
</code></pre>
<p>without the use of any explicit loops.</p>
<p>The expected output is,</p>
<pre class="lang-py prettyprint-override"><code>[3, 2, 5, 1, 1]
</code></pre>
<p>I am using Python and the NumPy library.</p>