Removing consecutive zeros of a certain length

5 ビュー (過去 30 日間)
Debbie Oomen
Debbie Oomen 2018 年 5 月 9 日
編集済み: Stephen23 2018 年 5 月 9 日
Hi everyone,
I have data consisting of two columns: time and counts. The counts data consists of multiple numbers with some consecutive zeros. How can Matlab find and count the duration of these consecutive zeros and then delete the rows of them if the duration is above a certain value?

採用された回答

KSSV
KSSV 2018 年 5 月 9 日
編集済み: KSSV 2018 年 5 月 9 日
x = [0 0 0 2 2 0 0 0 3 3 3 3 3 0 0 0 0 ]' ;
transitions = diff([0 ;x == 0; 0]); %find where the array goes from non-zero to zero and vice versa
runstarts = find(transitions == 1);
runends = find(transitions == -1); %one past the end
runlengths = runends - runstarts;
% keep only those runs of length 4 or more:
runstarts(runlengths > 3) = [];
runends(runlengths > 3) = [];
%expand each run into a list indices:
indices = arrayfun(@(s, e) s:e-1, runstarts, runends, 'UniformOutput', false);
indices = [indices{:}]; %concatenate the list of indices into one vector
% Remove those zeros which are consecuitve 3 in number
x(indices) = [] ;
  5 件のコメント
Stephen23
Stephen23 2018 年 5 月 9 日
You need to use the indices as row subscripts:
Data(indices,:) = [];
Debbie Oomen
Debbie Oomen 2018 年 5 月 9 日
編集済み: Stephen23 2018 年 5 月 9 日
Thank you! This works perfectly

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2018 年 5 月 9 日
編集済み: Jan 2018 年 5 月 9 日
What is the "duration"? The number of consecutive elements or does the "time" in the first column matter?
[B, N] = RunLength(Data(:, 2));
match = (B ~= 0 | N < 3);
index = RunLength(match, N);
Data2 = Data(index, :);
This removes all rows, if the number of consecutive zeros in the 2nd column is greater equal 3.
If you do not have a C-compiler installed, use RunLength_M() instead or RunLength() from the same submission. It does exactly the same as the faster C-Mex, but as slower Matlab code.

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by