# Finding longest consecutive numbers in array

41 ビュー (過去 30 日間)
John Doe 2018 年 1 月 27 日
コメント済み: John Doe 2018 年 1 月 28 日
Hello everyone,
This is somewhat of a silly question but I can't seem to figure it out. If I have an array of numbers what I want is to find the location of my longest consecutive numbers for example:
my arrary = [ 1999 2000 2001 2003 2004 2005 2006 2007];
I want my output to be = [ 4 5 6 7 8]; because that's the location of my longest consecutive numbers (2003-2007). I tried to find where difference is equal to 1 but then the result is [1 1 1 0 1 1 1 1] , it doesn't take the position of the last number which is 2007 here and even if I fix that problem also I'd still have to find the locations for the longest consecutive one's for the second scenario.
I could use some help in this,
Thank You!!!

サインインしてコメントする。

### 採用された回答

Stephen23 2018 年 1 月 27 日
>> V = [1999,2000,2001,2003,2004,2005,2006,2007];
>> D = diff(diff(V)==1);
>> B = find([true,D>0]);
>> E = find([D<0,true])+1;
>> [~,idx] = max(E-B);
>> B(idx):E(idx)
ans =
4 5 6 7 8
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
John Doe 2018 年 1 月 28 日
Thank You!!!!

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangePoint Cloud Processing についてさらに検索

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by