How to remove zero values from vector?
조회 수: 2 (최근 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.
댓글 수: 0
채택된 답변
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)];
댓글 수: 0
추가 답변 (2개)
belairaway
2017년 5월 9일
편집: belairaway
2017년 5월 9일
I think that using the built-in function nonzeros is better
댓글 수: 0
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)]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!