Sum colums of a matrix based on vector

username: dlmacedo
cooked: <p>Dear all,</p>
<p>Given a matrix A=MxM, I would like to construct a new matrix B=MxN, where each column of B is the sum of selected columns of A. The selection is given by a vector V of integer that determines the columns to be summed.</p>
<p>For example, if A is 128x128 and I have a vector V of 128 integers filled with number between 0 and 9, B should be 128x10 where the column i of B is equal to the sum of the columns of A where V[x]=i and x is the index of all columns to be summed.</p>
<p>Thanks,</p>
<p>David</p>

username: dlmacedo
cooked: <p>For example,</p>
<p>A = 5x6<br>
[<br>
[0 2 4 1 4 0]<br>
[2 3 4 5 1 1]<br>
[1 5 2 2 3 1]<br>
[2 5 1 2 3 0]<br>
[3 4 1 2 1 1]<br>
]</p>
<p>And V = [2 0 0 2 2 0]</p>
<p>Then</p>
<p>B = 5x4<br>
[<br>
[6 0 5 0]<br>
[8 0 8 0]<br>
[8 0 6 0]<br>
[6 0 7 0]<br>
[6 0 6 0]<br>
]</p>

username: dlmacedo
cooked: <p>Does anyone have any idea?</p>

username: ptrblck
cooked: <p>I don’t completely understand the use case.<br>
How is the shape of <code>B</code> created as <code>[5, 4]</code>, if <code>V</code> only contains values in <code>[0, 2]</code>.</p>
<aside class="quote no-group quote-modified" data-username="dlmacedo" data-post="1" data-topic="93152">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dlmacedo/48/1387_2.png" class="avatar"> dlmacedo:</div>
<blockquote>
<p>the column i of B is equal to the sum of the columns of A where V<span class="chcklst-box checked fa fa-check-square-o fa-fw"></span>=i and x is the index of all columns to be summed.</p>
</blockquote>
</aside>
<p>In your example, for <code>i=0</code>, this should be true:</p>
<pre data-code-wrap="python"><code class="lang-python">i = 0
B[:, i] = sum(A[:, V==i])
</code></pre>
<p>which would be a scalar output.<br>
Could you post a manual calculation of the result, so that we could come up with a code snippet? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: dlmacedo
cooked: <p>Thanks for answering!</p>
<p>The B has the same number of lines of A. This fact allows us to sum columns of A to construct B. So B has to have 5 lines. Think the number of lines as the number of training examples.</p>
<p>Think the number of columns of A as the number of features of the problem. For example, our example is considering we have 6 features. Therefore, the number of columns of A is 6.</p>
<p>Think the number of columns of B as the number of class of the problem. This example is considering we have, for instance, 4 classes. Therefore, B has 4 columns.</p>
<p><strong>In our problem, the size of the vector V is equal to the number of columns of A.  This value may be lower, equal or higher than the number of classes. Nevertheless, the size of V is equal to the number of columns of A.</strong></p>
<p>The V vector has only non-negative integer representing the targets classes (the columns of A to be summed to construct  the columns of B. Each column of B represents a class.).</p>
<p>Naturally, V can not have an integer bigger than the number of columns of B, which represents the number of classes. So, there is no problem in V only having the classes 0 and 2. This is the reason why B has the column 1 and 3 equals to zero (we have no columns to sum to targets 1 and 3).</p>

username: dlmacedo
cooked: <p>For example,</p>
<p>The first (0-index) column of B is the sum of the columns 1, 2 and 5 of A because V has 0 in these positions.</p>
<p>The second (1-index) column of B is zero because the V vector has no value 1.</p>
<p>The third (2-index) column of B is the sum of the columns 0, 3 and 4 of A because V has 2 in these positions.</p>
<p>The fourth (3-index) column of B is zero because the V vector has no value 3.</p>