Then you could use accumarray, which is intended for exactly this kind of operation. Here is a simple example, where each run of identical digits in A is used to sum the values in B (e.g. the first run of A (the nines) sum the first B values [1,2,3] to give six:
>> A = [9,9,9,1,2,2,1,1]';
>> B = [1,2,3,4,5,6,7,8]';
>> X = cumsum([1;0~=diff(A)]);
>> accumarray(X,B)
ans =
6
4
11
15
>> [C,~,Y] = unique(A);
>> accumarray(Y,B)
ans =
19
11
6
>> C
C =
1
2
9
Note in this case the ones are summed together, even though they occur in two different runs in A.