2014-11-21 3 views

답변

1

이렇게하면 이러한 모든 서브 시퀀스의 시작 인덱스를 줄 것이다 :

n = 4; 
indices = find(conv(double(diff(A)>0), ones(1,n-1), 'valid')==n-1); 

예 :

A = [8 9 1 3 7 18 9 10 11 12 5]; 

indices = 
    3  7 

을 생산 그래서 서브 시퀀스가 ​​될 것 A(indices(1) + (0:n-1)), A(indices(2) + (0:n-1)) 등 :

,210
>> A(indices(1) + (0:n-1)) 
ans = 
    1  3  7 18 

>> A(indices(2) + (0:n-1)) 
ans = 
    9 10 11 12 
2

strfind을 사용하면 문자열뿐만 아니라 숫자 배열도 찾을 수 있습니다.

A = [8 9 1 3 7 18 19] 

sequenceLength = 4; 

startIdx = strfind(sign(diff(A)), ones(1,sequenceLength-1)); 

sequences = A(bsxfun(@plus,startIdx',0:sequenceLength-1)) 

sequences = 

    1  3  7 18 
    3  7 18 19 

참고 : strfind 발견 겹치는 간격으로 당신이 찾고있는 패턴은 세 개의 연속적인 양의 차이입니다. 독점적 인 간격을 원할 경우 regexp을 참조하십시오.

+0

음'bsxfun' 부분을 알 –

3

또 다른 해결책 :

A = [8 9 1 3 7 18 9 10 11 12 5]; 
len = 4; 

subseqs = hankel(A(1:len), A(len:end)); 
idx = all(diff(subseqs) > 0); 
out = subseqs(:,idx); 
관련 문제