想象一下,您的序列非常長。什麼是尋找區間的最有效的方式,其中序列是全零(或更精確的順序下降到接近零值abs(X)<eps
):在序列中找到零的島嶼
爲了簡單起見,讓我們假設按以下順序:
sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];
我試圖獲得以下信息:
startIndex EndIndex Duration
3 6 4
12 12 1
14 16 3
25 26 2
30 30 1
然後利用這些信息,我們發現具有持續時間的間隔> =一些特定值(比如3
),並返回值的指數所有這些間隔聯合:
indices = [3 4 5 6 14 15 16];
這最後一部分是與先前的問題:
MATLAB: vectorized array creation from a list of start/end indices
這是我到目前爲止有:
sig = [1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0];
len = length(sig);
thresh = 3;
%# align the signal with itself successively shifted by one
%# v will thus contain 1 in the starting locations of the zero interval
v = true(1,len-thresh+1);
for i=1:thresh
v = v & (sig(i:len-thresh+i) == 0);
end
%# extend the 1's till the end of the intervals
for i=1:thresh-1
v(find(v)+1) = true;
end
%# get the final indices
v = find(v);
我正在尋找矢量化/優化代碼,但我願意接受其他解決方案附件。 我不得不強調空間和時間效率非常重要,因爲我正在處理大量長生物信號。
我喜歡你的單詞島的用法。 – ChaosPandion 2010-07-18 02:07:03
@ChaosPandion:在一片海中搜索零島.. arrr :) – merv 2010-07-18 22:03:28