Thanks for answering!
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).