title: Setting values of a tensor based on given indices of corresponding rows using pytorch
body: <p>I've got a tensor <code>A</code> with shape <code>(M, N)</code>, and have another tensor <code>B</code> with shape <code>(M, P)</code> and with values of given indices in corresponding rows of <code>A</code>. Now I would like to set the values of <code>A</code> with corresponding indices in <code>B</code> to <code>0</code>.</p>
  <p>For example:</p>
  <pre class="lang-py prettyprint-override"><code>In[1]: import torch
         A = torch.tensor([range(1,11), range(1,11), range(1,11)])
         A
  Out[1]: 
  tensor([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
          [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
          [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]])
  </code></pre>
  <pre class="lang-py prettyprint-override"><code>In[2]: B = torch.tensor([[1,2], [2,3], [3,5]])
         B
  Out[2]: 
  tensor([[1, 2],
          [2, 3],
          [3, 5]])
  </code></pre>
  <p>The objective is to set the value of the element with index <code>1,2</code> in the first row, <code>2,3</code> in the second row, and <code>3,5</code> in the third row of <code>A</code> to <code>0</code>, i.e., setting <code>A</code> to</p>
  <pre class="lang-py prettyprint-override"><code>tensor([[ 1,  0,  0,  4,  5,  6,  7,  8,  9, 10],
          [ 1,  2,  0,  0,  5,  6,  7,  8,  9, 10],
          [ 1,  2,  3,  0,  5,  0,  7,  8,  9, 10]])
  </code></pre>
  <p>I have applied row by row for loop, and also tried <code>scatter</code>:</p>
  <pre class="lang-py prettyprint-override"><code>zeros = torch.zeros(A.shape, dtype=torch.float).to(&quot;cuda&quot;)
  A = A.scatter_(1, B, zeros)
  </code></pre>
  <p>The two methods work fine, but all give quite poor performance. Actually, I infer that some efficient approach should exist based on an error before. I initially used <code>A[:, B] = 0</code>. This would set all the indices of appeared in <code>B</code> to <code>0</code>, regardless of the row. However, the training speed improved drastically when doing <code>A[:, B] = 0</code>.</p>
  <p>Is there any way to implement this more efficiently?</p>