猿问

在序列中查找零岛

在序列中查找零岛

想象一下你有一个很长的序列。找出序列全部为零的区间(或者更准确地说,序列降到近零值)的最有效的方法是什么?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:从开始/结束索引列表中创建向量化数组

到目前为止,这就是我所拥有的:


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);

我希望对代码进行矢量化/优化,但我对其他解决方案持开放态度。我必须强调,空间和时间效率是非常重要的,因为我正在处理大量的长生物信号。


蛊毒传说
浏览 484回答 3
3回答

一只斗牛犬

以下是我将采取的步骤,以矢量化的方式解决您的问题,从给定的向量开始。sig:首先,对向量进行阈值化,得到一个向量。tsig指零和一(信号绝对值降到接近于零的零点,其他地方的零点):tsig&nbsp;=&nbsp;(abs(sig)&nbsp;>=&nbsp;eps);&nbsp;&nbsp;%#&nbsp;Using&nbsp;eps&nbsp;as&nbsp;the&nbsp;threshold接下来,使用函数查找每个零字符串的起始索引、结束索引和持续时间。差夫和找到,发现:dsig&nbsp;=&nbsp;diff([1&nbsp;tsig&nbsp;1]);startIndex&nbsp;=&nbsp;find(dsig&nbsp;<&nbsp;0);endIndex&nbsp;=&nbsp;find(dsig&nbsp;>&nbsp;0)-1;duration&nbsp;=&nbsp;endIndex-startIndex+1;然后,查找持续时间大于或等于某个值的零字符串(如示例中的3):stringIndex&nbsp;=&nbsp;(duration&nbsp;>=&nbsp;3);startIndex&nbsp;=&nbsp;startIndex(stringIndex);endIndex&nbsp;=&nbsp;endIndex(stringIndex);最后,使用从我对链接问题的回答中找出的方法若要生成最后一组索引,请执行以下操作:indices&nbsp;=&nbsp;zeros(1,max(endIndex)+1);indices(startIndex)&nbsp;=&nbsp;1;indices(endIndex+1)&nbsp;=&nbsp;indices(endIndex+1)-1;indices&nbsp;=&nbsp;find(cumsum(indices));

慕斯709654

您可以通过查找长度为零的字符串,将其作为字符串搜索任务来解决。thresh(STRFIND函数非常快)startIndex&nbsp;=&nbsp;strfind(sig,&nbsp;zeros(1,thresh));请注意,较长的子字符串将在多个位置被标记,但当我们从间隔开始添加中间位置时,最终将连接到一起。startIndex到此为止start+thresh-1.indices&nbsp;=&nbsp;unique(&nbsp;bsxfun(@plus,&nbsp;startIndex',&nbsp;0:thresh-1)&nbsp;)';请注意,您始终可以将最后一步与CUMSUM/Find解决方案从关联问题.

芜湖不芜

function&nbsp;indice=sigvec(sig,thresh) &nbsp;&nbsp;&nbsp;&nbsp;%extend&nbsp;sig&nbsp;head&nbsp;and&nbsp;tail&nbsp;to&nbsp;avoid&nbsp;0&nbsp;head&nbsp;and&nbsp;0&nbsp;tail &nbsp;&nbsp;&nbsp;&nbsp;exsig=[1,sig,1]; &nbsp;&nbsp;&nbsp;&nbsp;%convolution&nbsp;sig&nbsp;with&nbsp;extend&nbsp;sig &nbsp;&nbsp;&nbsp;&nbsp;cvexsig=conv(exsig,ones(1,thresh)); &nbsp;&nbsp;&nbsp;&nbsp;tempsig=double(cvexsig==0); &nbsp;&nbsp;&nbsp;&nbsp;indice=find(conv(tempsig,ones(1,thresh)))-thresh;
随时随地看视频慕课网APP
我要回答