필터 지우기
필터 지우기

How to make my logical data homogene?

조회 수: 1 (최근 30일)
Max
Max 2015년 1월 6일
댓글: Image Analyst 2015년 1월 7일
Hi all,
I have a vector logical data which varies between 0 and 1 in this way:
data = [0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0];
the number of repetition of zeros and ones are irregular as seen above. My problem is that in some points some ONES are dropped out like this:
data = [0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 0 0];
As you see elements number 15, 22 and 35 are exchanged from 1 to 0 in compared to the previous vector. I could fix it with a for loop asking to replace 0 to 1 every time there is a 0 between two 1s. But I was looking for some other way to avoid for loop.
I really appreciate any help.

채택된 답변

Image Analyst
Image Analyst 2015년 1월 6일
As long as you never have a single isolated 1 surrounded by zeros that you want to keep, you can do it in a single line of code if you have the Signal Processing Toolbox:
fixedData = medfilt1(data)
  댓글 수: 1
Image Analyst
Image Analyst 2015년 1월 7일
By the way, if you did have single isolated 1's that you wanted to keep, you'd just OR the above answer with your original data:
data = [0 0 1 0 0 1 0 0 1 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 0 0];;
fixedData = medfilt1(data) | data
The median filter would get rid of those 3 isolated 1's in the beginning, but the ORing with the original data would put them back in. Clever, huh?

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

추가 답변 (2개)

Star Strider
Star Strider 2015년 1월 6일
I would use the filter function to find them:
data1 = [0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0];
data2 = [0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 0 0];
difdata2 = diff([0 data2]);
data2flt = filter([1 -1], 2, difdata2);
data2z = find(data2flt == 1)-1;
where ‘data2z’ are the indices of the positions you want.
The code is relatively uncomplicated. The ‘difdata2’ assignment simply computes the differences between the elements, with the initial zero resulting in its length being the same as ‘data2’. The ‘data2flt’ assignment uses filter to detect the desired patterns. Because of the way filter operates, this is offset by the length of the filter - 1. The ‘data2z’ assignment returns these positions, correcting for the offset. So long as your data conform to the data you’ve posted, this code should work.
This also works (note the absence of the offset, although ‘difdata2’ is now one element shorter than ‘data2’):
difdata2 = diff(data2);
data2flt = filter([1 -1], 2, difdata2);
data2z = find(data2flt == 1);
Much depends on your data and how you want to process it.
  댓글 수: 1
Max
Max 2015년 1월 7일
Thank you very much Star :)

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


Roger Stafford
Roger Stafford 2015년 1월 7일
data(diff([1,data,1],2)==2) = 1;
  댓글 수: 1
Max
Max 2015년 1월 7일
Thanks so much Roger. This is quiet smart.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by