2014-11-21 37 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))等:

>> 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'部分 – 2014-11-21 17:43:52

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