Eliminating zero's between rising and falling edge

조회 수: 2 (최근 30일)
pr
pr 2014년 3월 20일
답변: Roger Stafford 2014년 3월 21일
A series with 1's and 0's. a = [0 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 ]; and so on... Now I would like to eliminate the zeros in between 1's i.e debouncing and the desired output is: [0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0] . I achieved this using while loop. But if I run the code for about 100 thousand elements the program get's hanged and moreover takes lot of time. For eg: the index for rising edge is 4 and the falling edge is 11, now the zero's between them should be made equal to 1. In the sequence there are atleast 5 or more zeros between the two rising edges.
Would any one suggest the easiest way to solve this?

채택된 답변

Roger Stafford
Roger Stafford 2014년 3월 21일
It isn't clear just how many consecutive zeros are required for them not to be changed to ones. Your statement "there are atleast 5 or more zeros between the two rising edges" hints at maybe 5, but you should state it clearly to avoid misunderstandings. Here is a rather cumbersome vectorized method, and I don't guarantee it is better than your 'while' loop.
f = find(diff([0,a,0])~=0);
f1 = f(2:2:end-2);
f2 = f(3:2:end-1);
t = (f2-f1)<5;
b = zeros(size(a));
b(f1(t)) = 1;
b(f2(t)) = -1;
b = a+cumsum(b);

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 3월 20일
If you have the Image Processing Toolbox, it's trivial - just a single line of code:
b = ~bwareaopen(~a, 3);
where 3 is the longest length of 0's that you want to allow. 100 thousand elements is no problem - this is just a very small fraction of the number of elements it usually works on (tens of millions of elements or pixels), so it will be pretty fast. If you don't have that toolbox , there are some trickier, more complicated ways to do it. Let us know and someone will probably work it out for you.

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by