蠱毒傳說
2019-07-09 14:35:47
在序列中查找零島想象一下你有一個很長的序列。找出序列全部為零的區(qū)間(或者更準確地說,序列降到近零值)的最有效的方法是什么?abs(X)<eps):為了簡單起見,讓我們假設(shè)以下順序: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然后使用這些信息,我們發(fā)現(xiàn)持續(xù)時間>=的時間間隔為某些指定的值(例如3),并返回所有這些區(qū)間中的值的索引組合:indices = [3 4 5 6 14 15 16];最后一部分與前一個問題有關(guān):MATLAB:從開始/結(jié)束索引列表中創(chuàng)建向量化數(shù)組到目前為止,這就是我所擁有的: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);我希望對代碼進行矢量化/優(yōu)化,但我對其他解決方案持開放態(tài)度。我必須強調(diào),空間和時間效率是非常重要的,因為我正在處理大量的長生物信號。
3 回答

一只斗牛犬
TA貢獻1784條經(jīng)驗 獲得超2個贊
sig
:
首先,對向量進行閾值化,得到一個向量。 tsig
指零和一(信號絕對值降到接近于零的零點,其他地方的零點): tsig = (abs(sig) >= eps); %# Using eps as the threshold
接下來,使用函數(shù)查找每個零字符串的起始索引、結(jié)束索引和持續(xù)時間。 差夫 和 找到,發(fā)現(xiàn) :dsig = diff([1 tsig 1]);startIndex = find(dsig < 0);endIndex = find(dsig > 0)-1;duration = endIndex-startIndex+1;
然后,查找持續(xù)時間大于或等于某個值的零字符串(如示例中的3): stringIndex = (duration >= 3);startIndex = startIndex(stringIndex);endIndex = endIndex(stringIndex);
最后,使用 從我對鏈接問題的回答中找出的方法 若要生成最后一組索引,請執(zhí)行以下操作: indices = zeros(1,max(endIndex)+1);indices(startIndex) = 1;indices(endIndex+1) = indices(endIndex+1)-1;indices = find(cumsum(indices));

慕斯709654
TA貢獻1840條經(jīng)驗 獲得超5個贊
thresh
startIndex = strfind(sig, zeros(1,thresh));
startIndex
start+thresh-1
.
indices = unique( bsxfun(@plus, startIndex', 0:thresh-1) )';

蕪湖不蕪
TA貢獻1796條經(jīng)驗 獲得超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
提交
取消