次のコードは、4
の倍数に近いギャップの最長系列に属する時間タグを検索しアルゴリズムは、有効なギャップがシリーズから欠落している可能性があると仮定します(連続性を検索しません)。
%T is the X coordinate of your graph (time tag).
%Notice: The amplitude is irrelevant here.
T = [1, 2, 5, 6, 7, 10, 12, 14];
%Create all possible combinations of indexes of T.
[Y, X] = meshgrid(1:length(T));
%G matrix is the combinations of all gaps:
%T(1) - T(1), T(2) - T(1), T(3) - T(1)...
%It is inefficient to compute all gaps (even in reverse and T(1) - T(1)),
%But it is a common way to solve problems using Matlab.
G = T(X) - T(Y);
%Ignore sign of gaps.
G = abs(G);
%Remove all gaps that are not multiple of 4 with 0.1 hysteresis.
%Remove gaps like 5, 11, and 12.7...
G((mod(G, 4) > 0.1) & (mod(G, 4) < 3.9)) = 0;
%C is a counter vector - counts all gaps that are not zeros.
%Now C holds the number of elements in the relevant series of each time sample.
C = sum(G > 0, 1);
%Only indexes belongs to the maximum series are valid.
ind = (C == max(C));
%Result: time tags belongs to the longest series.
resT = T(ind)
注:それを行いますこと、
T = [1, 2, 5, 6, 7, 10, 12, 14];
len = length(T);
C = zeros(1, len);
for i = 1:len-1
j = i;
k = i+1;
while (k <= len)
gap = T(k) - T(j);
if (abs(gap - 4) < 0.1)
C(i) = C(i) + 1; %Increase series counter.
%Continue searching from j forward.
j = k;
k = j+1;
else
k = k+1;
end
if (gap > 4.1)
%Break series if gap is above 4.1
break;
end
end
end
%now find(C == max(C)) is the index of the beginning of the longest contentious series.
まあ:あなたが隙間なく最長のシリーズを探している場合は
次のコードを使用することができます!どうもありがとう! :) – ebernardes
喜んでください...私の解決策はすべてのケースをカバーしていないことに注意してください。例えば、2つの非常に近いパルスが間違って数えられるかもしれません。私もそれを徹底的にテストしなかった。 – Rotem
最初の1つまたは2つ目のどちらのソリューションが好きですか? – Rotem