How to select elements in an array that are in sequence?

12 ビュー (過去 30 日間)
Soumyatha Gavvala
Soumyatha Gavvala 2016 年 6 月 17 日
コメント済み: Soumyatha Gavvala 2016 年 6 月 17 日
I have a dynamic array which goes like this [1 2 3 4 31 32 33 34 35 36 37 38 39 40 41 42 56 57 58 59]. Where there is a larger set of elements in sequence (31 to 42) and smaller sets of elements (1 to 4 and 56 to 59). And these values change for every run.
I want to able to select the larger set of elements in sequence. Here, (31 to 42). Is there any way I can do that?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 6 月 17 日
a=[1 2 3 4 31 32 33 34 35 36 37 38 39 40 41 42 56 57 58 59]
id=[1 diff(a)]==1
ii1=strfind([0 id],[0 1])
ii1(2:end)=ii1(2:end)-1
ii2=strfind([id 0],[1 0])
[~,jd]=max(ii2-ii1)
out=a(ii1(jd):ii2(jd))
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 6 月 17 日
How it doesn't work?
Soumyatha Gavvala
Soumyatha Gavvala 2016 年 6 月 17 日
Thank you it works! I just had to use transpose to change the array dimensions.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by