Borrar filtros
Borrar filtros

How to remove zero values from vector?

2 visualizaciones (últimos 30 días)
KH
KH el 14 de Jun. de 2013
Editada: belairaway el 9 de Mayo de 2017
Hi,
Let's say I have vector something like:
q = [0 0 0 2 4 10 9 5 0 0 0 8 9]
I want it to be something like:
q = {0 0 0 2 4 10 9 5 8 9]
How can I choose to remove the zero values but without removing the first three zeroes?
Thank you.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 14 de Jun. de 2013
q = [0 0 0 2 4 10 9 5 0 0 0 8 9];
out = [q(1:find(q~=0,1,'first')-1) q(q~=0)];

Más respuestas (2)

belairaway
belairaway el 9 de Mayo de 2017
Editada: belairaway el 9 de Mayo de 2017
I think that using the built-in function nonzeros is better

Azzi Abdelmalek
Azzi Abdelmalek el 14 de Jun. de 2013
Editada: Azzi Abdelmalek el 14 de Jun. de 2013
q = [0 0 0 2 4 10 9 5 0 0 0 8 9];
out=[q(1:find(q~=0,1)) q(q~=0)]

Categorías

Más información sobre Get Started with MATLAB 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!

Translated by