How to filter out single/double zero's ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
JamJan
el 23 de Sept. de 2019
Comentada: Guillaume
el 23 de Sept. de 2019
A = [1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0]
I have vector A and I want to smoothen the vector by changing the single 0's or double 0's to 1's. This means that I want to keep the consecutive zero's when bigger than 2 consecutive.
Is there an easy way to do this without using a for loop?
Thanks
0 comentarios
Respuesta aceptada
Fabio Freschi
el 23 de Sept. de 2019
% find indices
idx1 = strfind([0 A == 0 0],[0 1 0]);
idx2 = strfind([0 A == 0 0],[0 1 1 0]);
% replace
A(idx1) = 1;
A([idx2(:); idx2(:)+1]) = 1;
1 comentario
Guillaume
el 23 de Sept. de 2019
Note that, while this is a good solution, this usage of strfind with numeric arrays is undocumented. I have suggested to Mathworks to make it documented (or make a function that does the same for numeric vectors), but for now, use at your own perils. It may stop working in future versions.
Más respuestas (2)
Guillaume
el 23 de Sept. de 2019
If you have the Image Processing Toolbox, the easiest is:
newA = imclose(A, [1 1 1])
0 comentarios
Ver también
Categorías
Más información sobre Filter Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!