2014-11-21 82 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);