matlab - cumsum only within groups? -


lets 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 want cumsum, result should that:

c=[1 3;2 1;3 3] 

that means have ones in b 3 ones in a, group 2 in b have 1 one in etc.

if you're looking solution b can anything, combination of hist , unique help:

num = unique(b(logical(a))); %# identify numbers in b non-zero counts cts = hist(b(logical(a)),num); %# count c = [num(:),cts(:)]; %# combine.  

if want first column of c go 1 maximum of b, can rewrite first line num=1:max(b), , you'll rows in c counts zero.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -