필터 지우기
필터 지우기

Deleting runs of zeros

조회 수: 2 (최근 30일)
Lauren
Lauren 2015년 5월 14일
댓글: Lauren 2015년 5월 14일
Hello! I have a dataset that occasionally has runs of zeros. I would like to delete those runs from my data. How do I state that for any run of 3 or more zeros in my data, I would like Matlab to completely delete those observations from my dataset.
So for example, I have a dataset that looks like
[1 5;2 7;3 0;4 0;5 0;6 3]
I would like the that run of zeros deleted so that my new dataset looks like...
[1 5;2 7;6 3]
Any help is greatly appreciated!!

채택된 답변

Guillaume
Guillaume 2015년 5월 14일
one possible way, use strfind (which works with numbers despite the name). Using a more complex example (to show that it works regardless of the numbers of 0 in the run):
data = [1 5; 2 0; 3 0; 4 2; 5 3; 6 0; 7 0; 8 0; 9 0; 10 1; 11 0; 12 0; 13 0]
startseq = strfind(data(:, 2)', [0 0 0]);
toremove = unique([startseq, startseq+1, startseq+2]);
data(toremove, :) = []
  댓글 수: 1
Lauren
Lauren 2015년 5월 14일
Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Doug
Doug 2015년 5월 14일

a = [1 5;2 7;3 0;4 0;5 0;6 3];

i = a(:,2)~=0;

a = a(i,:);

  댓글 수: 1
Lauren
Lauren 2015년 5월 14일
This code removes all zeros from my dataset. I only want to remove runs of 3 or more consecutive zeros. Can you modify this code to reflect that?

댓글을 달려면 로그인하십시오.


Joseph Cheng
Joseph Cheng 2015년 5월 14일
you would find the indexes that equal zero then if the number is greater or equal to three then delete them from original like in the example below;
x = [1 5;2 7;3 0;4 0;5 0;6 3]
[r c v]=find(x==0);
if numel(r)>=3
x(r,:)=[];
end
  댓글 수: 2
Stephen23
Stephen23 2015년 5월 14일
@ Joseph Cheng: do not put empty lines in your code, and then this page will format the entire code in one box.
Also while this certainly detects zeros, it does not detect runs of zeros, which by definition must be consecutive. This code does not resolve this issue.
Joseph Cheng
Joseph Cheng 2015년 5월 14일
?when did the page layout change? oh and yes i didn't see the comment on runs of zeros. However quick modification of taking the diff of r (diff(r)) and looking for values of 1, one can determine runs of zeros.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Predictive Maintenance Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by