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.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 6월 14일

0 개 추천

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일

1 개 추천

I think that using the built-in function nonzeros is better
Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 14일
편집: Azzi Abdelmalek 2013년 6월 14일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

KH
2013년 6월 14일

편집:

2017년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by