How to find the second zero element in an array?

16 Ansichten (letzte 30 Tage)
Leo Müller
Leo Müller am 11 Nov. 2015
Kommentiert: Leo Müller am 11 Nov. 2015
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!

Akzeptierte Antwort

Mohammad Abouali
Mohammad Abouali am 11 Nov. 2015
Bearbeitet: Mohammad Abouali am 11 Nov. 2015
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by