deleting separate zeros from vector
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Legally Blonde
el 6 de Oct. de 2021
Comentada: Legally Blonde
el 6 de Oct. de 2021
I need to remove separate zeros from a series of numbers. But if zeros repeat, I need to keep them.
example of vector:
A = [ 0 0 1 0 0 0 1 0 1 0 0 1 5 9 8 2 0 3 0 1 0 0 0 ];
result:
A = [ 0 0 1 0 0 0 1 1 0 0 1 5 9 8 2 3 1 0 0 0 ];
0 comentarios
Respuesta aceptada
Rik
el 6 de Oct. de 2021
It took a bit of thinking, but here is a oneliner:
A = [ 0 0 1 0 0 0 1 0 1 0 0 1 5 9 8 2 0 3 0 1 0 0 0 ];
A(conv([NaN A]==0,[-1 1 -1])==1)=[];
A
Thanks to @Alan Stevens for the idea of converting A to a logical and ignoring it for the rest of the processing.
Más respuestas (1)
Alan Stevens
el 6 de Oct. de 2021
Here's one way (there is probably a slicker way!):
A = [ 0 0 1 0 0 0 1 0 1 0 0 1 5 9 8 2 0 3 0 1 0 0 0 ];
ix = find(A~=0);
it = find(abs(diff(ix))==2);
remove = ix(it+1)-1;
A(remove)=[]
0 comentarios
Ver también
Categorías
Más información sobre Time Series Events 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!