display 4 consecutive numbers in array

I have the Array a= 9 10 11 12 17 18 19 20 23 24 How can i get the code to display when the 4 indices are consecutive and when they appear. example: 9 10 11 12 and 17 18 19 20

 採用された回答

Guillaume
Guillaume 2014 年 10 月 8 日

1 投票

Strangely enough, strfind also find patterns in numeric arrays:
a = [9 10 11 12 17 18 19 20 23 24];
indices = strfind(diff(a), [1 1 1])
See also Loren's blog on the subject of finding patterns for alternative algorithms.

1 件のコメント

Stephen23
Stephen23 2014 年 10 月 8 日
Very tidy solution.

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

その他の回答 (1 件)

David Sanchez
David Sanchez 2014 年 10 月 8 日

0 投票

a= [9 10 11 12 17 18 19 20 23 24];
for k=1:(length(a)-3) % k=2
if a(k:k+3)==(a(k):a(k)+3)
fprintf('repeition in position %g: \n',k);
disp(a(k:k+3));
end
end

2 件のコメント

Kole
Kole 2014 年 10 月 8 日
Error using == Matrix dimensions must agree.
Error in hw3 (line 7) if a(i:i+3)==(a(i):(a(i)+3))
Kole
Kole 2014 年 10 月 8 日
編集済み: Kole 2014 年 10 月 8 日
this is what i was going for but couldnt get it to work

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

カテゴリ

ヘルプ センター および File ExchangeData Types についてさらに検索

質問済み:

2014 年 10 月 8 日

編集済み:

2014 年 10 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by