フィルターのクリア

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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by