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.

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.

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.

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.

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.

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.

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.).

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).