필터 지우기
필터 지우기

How to remove zero values from vector?

조회 수: 2 (최근 30일)
KH
KH 2013년 6월 14일
편집: belairaway 2017년 5월 9일
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by