Lets say I have 2 vectors:
a=[0 1 0 1 1 0 1 0 0 0 1 1 1];
b=[1 1 1 1 1 1 2 2 2 3 3 3 3];
For every group of numbers in b I want to cumsum, so that the result should look like that:
c=[1 3;2 1;3 3]
That means that I have for the ones in b 3 ones in a, for group two in b I have only one one in a etc.
If you’re looking for a solution where
bcan be anything, then a combination ofhistanduniquewill help:If you want the first column of
cto go from 1 to the maximum ofb, then you can rewrite the first line asnum=1:max(b), and you’ll also get rows incwhere the counts are zero.