title: Pytorch - how do I index a 2-D matrix row wise?
body: <p>I would like to index a 2-D matrix row-wise and re-assign values.</p>
  
  <p>For example, first consider a 1-D vector case where we have three 1-D tensors <code>t1, indexes, t2</code> with the same shape. We can do this indexing and re-assignment as follows:</p>
  
  <pre><code>indexes = torch.tensor([0, 2, 1, 3])
  t1 = torch.tensor([0.0, 0.0, 0.0, 0.0])
  t2 = torch.tensor([0.1, 0.2, 0.3, 0.4])
  
  t1[indexes] = t2
  </code></pre>
  
  <p>Now, say that <code>t1, indexes, t2</code> are 2-D matrices instead of 1-D vectors and have the same shape <code>(R X C)</code>. I would like to do similar indexing as above for every row in these matrices where:</p>
  
  <pre><code>for i in range(R):
      t1[i][indexes[i]] = t2[i]
  </code></pre>
  
  <p>I would like to vectorize this operation instead of using a for loop. How do I do this?</p>