How to find the second zero element in an array?

조회 수: 8 (최근 30일)
Leo Müller
Leo Müller 2015년 11월 11일
댓글: Leo Müller 2015년 11월 11일
Hello, I have the following problem: I would like to find the second non zero element in an array such as: array = [ 0 138 49 47 0 0 0 0] What I want is the index 5. Thank you for your help!

채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 11월 11일
편집: Mohammad Abouali 2015년 11월 11일
index 5 is zero? You said you are looking for non-zero?!!!
anyway you can use find().
something like this:
myArray=[ 0 138 49 47 0 0 0 0];
idx=find(myArray==0,2,'first');
if (numel(idx)<2)
error('less than 2 elements were found');
end
idx=idx(end)
idx =
5
  댓글 수: 1
Leo Müller
Leo Müller 2015년 11월 11일
Thank you, this is what I was looking for!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by