display 4 consecutive numbers in array

3 Ansichten (letzte 30 Tage)
Kole
Kole am 8 Okt. 2014
Bearbeitet: Kole am 8 Okt. 2014
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

Akzeptierte Antwort

Guillaume
Guillaume am 8 Okt. 2014
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.

Weitere Antworten (1)

David Sanchez
David Sanchez am 8 Okt. 2014
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 Kommentare
Kole
Kole am 8 Okt. 2014
Bearbeitet: Kole am 8 Okt. 2014
this is what i was going for but couldnt get it to work

Melden Sie sich an, um zu kommentieren.

Kategorien

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by