How to make my logical data homogene?

1 visualización (últimos 30 días)
Max
Max el 6 de En. de 2015
Comentada: Image Analyst el 7 de En. de 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.

Respuesta aceptada

Image Analyst
Image Analyst el 6 de En. de 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 comentario
Image Analyst
Image Analyst el 7 de En. de 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?

Iniciar sesión para comentar.

Más respuestas (2)

Star Strider
Star Strider el 6 de En. de 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 el 7 de En. de 2015
data(diff([1,data,1],2)==2) = 1;
  1 comentario
Max
Max el 7 de En. de 2015
Thanks so much Roger. This is quiet smart.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by