How to remove zero values from vector?

3 次查看(过去 30 天)
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.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-6-14
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)];

更多回答(2 个)

belairaway
belairaway 2017-5-9
编辑:belairaway 2017-5-9
I think that using the built-in function nonzeros is better

Azzi Abdelmalek
Azzi Abdelmalek 2013-6-14
编辑:Azzi Abdelmalek 2013-6-14
q = [0 0 0 2 4 10 9 5 0 0 0 8 9];
out=[q(1:find(q~=0,1)) q(q~=0)]

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by