How to make my logical data homogene?

1 visualizzazione (ultimi 30 giorni)
Max
Max il 6 Gen 2015
Commentato: Image Analyst il 7 Gen 2015
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.

Risposta accettata

Image Analyst
Image Analyst il 6 Gen 2015
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 Commento
Image Analyst
Image Analyst il 7 Gen 2015
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?

Accedi per commentare.

Più risposte (2)

Star Strider
Star Strider il 6 Gen 2015
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.

Roger Stafford
Roger Stafford il 7 Gen 2015
data(diff([1,data,1],2)==2) = 1;

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by