I have a matrix A
in Matlab. I would like your help to find an
algorithm summing the columns of
A
indexed by the same integer.algorithm deleting the columns of
A
indexed by the same integer and putting instead one column of zero.
I thought about collapsing in one my two questions because I believe the solutions for the two algorithms should be similar once we've learnt how to pick the correct columns of A
.
Let me explain better with an example.
clear
b=8;
g=3;
B=[1;2;2;2;3;4;4;5]; %bx1
bnew=size(unique(B),1);
A=[1 2 3 4 5 6 7 8;
9 10 11 12 13 14 15 16;
17 18 19 20 21 22 23 24]; %gxb
Regarding the first algorithm: the matrix B
tells us which columns of A
should be summed among each other. For example: the second, the third, and the fourth element of B
are equal; this means that the second, the third, and the fourth columns of A
should be summed.
Regarding the second algorithm: the matrix B
tells us which columns of A
should be deleted and replaced with a column of zeros. For example: the second, the third, and the fourth element of B
are equal; this means that the second, the third, and the fourth columns of A
should be deleted and one column of zeros should be added.
It is important to notice that the columns to sum up or to delete are always adjacent. In other words, we cannot have for example
B=[1;2;2;2;3;2;4;5];
These are the matrices I want to get and I couldn't find how.
Matrixdesired1=[1 2+3+4 5 6+7 8;
9 10+11+12 13 14+15 16;
17 18+19+20 21 22+23 24]; %gxbnew
Matrixdesired2=[1 0 5 0 8;
9 0 13 0 16;
17 0 21 0 24]; %gxbnew