Assign sequential count for numerical runs
问题 I'd like to assign a cumulative numerical value for sequential runs in a binary vector. What I have is x = [0 0 0 1 1 0 1 1 1 0 1 0 0 0 0 0 0], and what I would like is y = [1 2 3 1 2 1 1 2 3 1 1 1 2 3 4 5 6]. The solution using sum/cumsum/unique/find range of functions alludes me. Any help would be greatly appreciated. 回答1: Here's a way: a = arrayfun(@(x)(1:x), diff(find([1,diff(x),1])), 'uni', 0); [a{:}] The idea is to generate a list of the 'run lengths', i.e. [3,2,1,3,1,1,6] in your case,