poster
2019-12-10, 20:48
假设您的序列很长。查找序列全为零的时间间隔(或更准确地说,序列下降到接近零的值abs(X) =的间隔达到某个指定值(例如3 ),并在所有这些间隔组合中返回值的索引:
indices = [3 4 5 6 14 15 16]; 最后一部分与上一个问题有关:
MATLAB:从开始/结束索引列表创建向量化数组 (https://stackoverflow.com/questions/2807270/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); 我正在寻求矢量化/优化代码,但是我对其他解决方案持开放态度。我必须强调空间和时间效率非常重要,因为我正在处理大量的长生物信号。
回答:
从给定的vector sig开始,我将按照以下这些步骤以向量化的方式解决您的问题:
首先,对向量进行阈值处理以获得零和一的向量tsig (信号绝对值下降到接近零的零,其他地方为零):
tsig = (abs(sig) >= eps); %# Using eps as the threshold
接下来,使用函数DIFF (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/diff.html)和FIND查找 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html)每个零字符串的开始索引,结束索引和持续时间:
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);
最后,使用我对所链接问题的答案中的方法 (https://stackoverflow.com/questions/2807270/matlab-vectorized-array-creation-from-a-list-of-start-end-indices/2807994#2807994)来生成最终的索引集:
indices = zeros(1,max(endIndex)+1); indices(startIndex) = 1; indices(endIndex+1) = indices(endIndex+1)-1; indices = find(cumsum(indices));
更多&回答... (https://stackoverflow.com/questions/3274043)
indices = [3 4 5 6 14 15 16]; 最后一部分与上一个问题有关:
MATLAB:从开始/结束索引列表创建向量化数组 (https://stackoverflow.com/questions/2807270/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); 我正在寻求矢量化/优化代码,但是我对其他解决方案持开放态度。我必须强调空间和时间效率非常重要,因为我正在处理大量的长生物信号。
回答:
从给定的vector sig开始,我将按照以下这些步骤以向量化的方式解决您的问题:
首先,对向量进行阈值处理以获得零和一的向量tsig (信号绝对值下降到接近零的零,其他地方为零):
tsig = (abs(sig) >= eps); %# Using eps as the threshold
接下来,使用函数DIFF (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/diff.html)和FIND查找 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html)每个零字符串的开始索引,结束索引和持续时间:
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);
最后,使用我对所链接问题的答案中的方法 (https://stackoverflow.com/questions/2807270/matlab-vectorized-array-creation-from-a-list-of-start-end-indices/2807994#2807994)来生成最终的索引集:
indices = zeros(1,max(endIndex)+1); indices(startIndex) = 1; indices(endIndex+1) = indices(endIndex+1)-1; indices = find(cumsum(indices));
更多&回答... (https://stackoverflow.com/questions/3274043)