How to remove zero values from vector?

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.

 Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 14 Jun. 2013

0 Stimmen

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)];

Weitere Antworten (2)

belairaway
belairaway am 9 Mai 2017
Bearbeitet: belairaway am 9 Mai 2017

1 Stimme

I think that using the built-in function nonzeros is better
Azzi Abdelmalek
Azzi Abdelmalek am 14 Jun. 2013
Bearbeitet: Azzi Abdelmalek am 14 Jun. 2013

0 Stimmen

q = [0 0 0 2 4 10 9 5 0 0 0 8 9];
out=[q(1:find(q~=0,1)) q(q~=0)]

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Gefragt:

KH
am 14 Jun. 2013

Bearbeitet:

am 9 Mai 2017

Community Treasure Hunt

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

Start Hunting!

Translated by