指定数值连续运行数 [英] 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],
和我想是
y = [1 2 3 1 2 1 1 2 3 1 1 1 2 3 4 5 6].
使用金额/ cumsum /独特的解决方案/查找功能范围暗示我。任何帮助将大大AP preciated。
The solution using sum/cumsum/unique/find range of functions alludes me. Any help would be greatly appreciated.
推荐答案
下面是一个方法:
a = arrayfun(@(x)(1:x), diff(find([1,diff(x),1])), 'uni', 0);
[a{:}]
我们的想法是产生运行长度的列表,即 [3,2,1,3,1,1,6]
在你的情况,然后就串联了一堆的载体,在该列表中算到每一个值,即猫(2,1:3,1:2,1:1,1:3 ...
我用 arrayfun
为重新应用快捷方式:
运营商,然后用逗号分隔的列表, {:}
收益作为串联一个快捷键
The idea is to generate a list of the 'run lengths', i.e. [3,2,1,3,1,1,6]
in your case, then just concatenate a bunch of vectors that count to each value in that list, i.e. cat(2, 1:3, 1:2, 1:1, 1:3...
. I use arrayfun
as a shortcut for reapplying the :
operator and then use the comma separated list that {:}
returns as a shortcut for the concatenation.
这篇关于指定数值连续运行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!