在序列中查找零島想象一下你有一個很長的序列。找出序列全部為零的區間(或者更準確地說,序列降到近零值)的最有效的方法是什么?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 Duration3 6 412 12 114 16 325 26 230 30 1然后使用這些信息,我們發現持續時間>=的時間間隔為某些指定的值(例如3),并返回所有這些區間中的值的索引組合:indices = [3 4 5 6 14 15 16];最后一部分與前一個問題有關:MATLAB:從開始/結束索引列表中創建向量化數組到目前為止,這就是我所擁有的: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 intervalv = 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 intervalsfor i=1:thresh-1 v(find(v)+1) = true;end%# get the final indicesv = find(v);我希望對代碼進行矢量化/優化,但我對其他解決方案持開放態度。我必須強調,空間和時間效率是非常重要的,因為我正在處理大量的長生物信號。
3 回答
一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
sig:
首先,對向量進行閾值化,得到一個向量。 tsig指零和一(信號絕對值降到接近于零的零點,其他地方的零點): tsig = (abs(sig) >= eps); %# Using eps as the threshold
接下來,使用函數查找每個零字符串的起始索引、結束索引和持續時間。 差夫 和 找到,發現 :dsig = diff([1 tsig 1]);startIndex = find(dsig < 0);endIndex = find(dsig > 0)-1;duration = endIndex-startIndex+1;
然后,查找持續時間大于或等于某個值的零字符串(如示例中的3): stringIndex = (duration >= 3);startIndex = startIndex(stringIndex);endIndex = endIndex(stringIndex);
最后,使用 從我對鏈接問題的回答中找出的方法 若要生成最后一組索引,請執行以下操作: indices = zeros(1,max(endIndex)+1);indices(startIndex) = 1;indices(endIndex+1) = indices(endIndex+1)-1;indices = find(cumsum(indices));
蕪湖不蕪
TA貢獻1796條經驗 獲得超7個贊
function indice=sigvec(sig,thresh) %extend sig head and tail to avoid 0 head and 0 tail exsig=[1,sig,1]; %convolution sig with extend sig cvexsig=conv(exsig,ones(1,thresh)); tempsig=double(cvexsig==0); indice=find(conv(tempsig,ones(1,thresh)))-thresh;
添加回答
舉報
0/150
提交
取消
