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개)

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 Exchange에서 Data 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