连续编号的Max系列 [英] Max Series of Consecutive Numbers
问题描述
我需要帮助找到一种方式来做到这一点,但它似乎应该是pretty简单。可以说我有一个 NX1
阵列。例如,让 X = [1 1 1.5 0.4 0.2 -.2 -.3 1 1 0 1]
。我所试图做的是找到最大的地方一系列连续 1
的开始,多少 1
的是在它。例如,使用 X
,最大的一系列连续系列开始索引1处,是长度3的同时,我将使用非常大的数据集,所以我会喜欢尝试并找到最有效和最快的方式可以做到这一点。
I need help finding a way to do this, although it seems as it should be pretty simple. Lets say I have a nx1
array. For example, let X=[1 1 1 .5 .4 .2 -.2 -.3 1 1 0 1]
. What I am trying to do is find where the greatest series of consecutive 1
's begins and how many 1
's are in it. For instance, using X
, the greatest series of consecutive series starts at the index 1 and is of length 3. Also, I will be using very large sets of data so I would like to try and find the most efficient and fastest way this can be done.
推荐答案
下面是一个你可以完成的相当有效的一种方法:
Here's one way that you could accomplish that fairly efficiently:
x = [1 1 1 0.5 0.4 0.2 -0.2 -0.3 1 1 0 1];
x1 = (x==1);
d = diff(x1(:).');
start = find([x1(1) d]==1)
len = find([d -x1(end)]==-1)-start+1
返回
start =
1 9 12
len =
3 2 1
这篇关于连续编号的Max系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!