查找相同值的系列

时间:2017-03-04 16:50:08

标签: matlab series

给定一个包含数字序列的向量A

目标是找到包含相同值的所有系列(长于给定数字"阈值")。结果应该是该系列的第一个和最后一个值的位置。

示例:给出一个向量A,其中:

A = [1 1 1 2 1 3 3 3 1 1 1 1 1 4 3 2 2 2 2 2 2 2 3 4];

和阈值B = 5;

结果将是:

[9 13]  % a series contain only the number 1 with length equal to 5
[16 22] % a series contain only the number 2 with length equal to 7

2 个答案:

答案 0 :(得分:1)

 A=[1 1 1 2 1 3 3 3 1 1 1 1 1 4 3 2 2 2 2 2 2 2 3 4];
 B = 5;

[l c]= size(A);                   % to know the size of 'A'
K=1;                              % to define the length of the series 
W=1;                              % a value used to save the positions of the wanted series.

For i=1:c-1
      If A(i)==A(i+1)  
          K=k+1;
       Else 
              If  k>= B %  to test of the actual series is equal or longer than the given threshold
                  S(w,1)=i;  
                  S(w,2)= S(w,1)-k+1;  % saving the first position and the last position of the series in 'S'
                   w=w+1;
       end
        k=1;
end

S %                       the final result which is a table contain all wanted series.  

结果如下:

S  13   9    % 13: the last position of the wanted series and 9 is the first position
   16  22

这一项工作太好了。但是,当它来到一张大桌子时,它太慢了。

答案 1 :(得分:1)

一种更快的矢量化方法是修改this solution中用于查找零岛的方法:

A = [1 1 1 2 1 3 3 3 1 1 1 1 1 4 3 2 2 2 2 2 2 2 3 4];  % Sample data
B = 5;  % Threshold

tsig = (diff(A) ~= 0);
dsig = diff([1 tsig 1]);
startIndex = find(dsig < 0);
endIndex = find(dsig > 0)-1;
duration = endIndex-startIndex+1;
stringIndex = (duration >= (B-1));
result = [startIndex(stringIndex); endIndex(stringIndex)+1].';

结果:

result =

     9    13
    16    22