How do you find the index of the first true value in a logical vector?
758 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jessica
am 18 Sep. 2014
Kommentiert: Alexander
am 17 Jul. 2022
I need to find the index of the first 1 in a logical vector.
For example, I have a logical vector like this: V = [ 0 1 0 0 1 0 1 ] and I need a function/code that will tell me that the first true value is at index 2.
(If you can't tell from my question, I'm a beginner at Matlab.) Thanks so much for your help! I really appreciate it :)
0 Kommentare
Akzeptierte Antwort
Star Strider
am 18 Sep. 2014
Use the find function:
V1 = find(V, 1, 'first')
The find function (in its most fundamental application) locates all non-zero entries in its argument. The syntax here tells it to locate only one such value, in this instance the first one it finds, and output the index of that value.
3 Kommentare
Star Strider
am 18 Sep. 2014
My pleasure!
(The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.)
Weitere Antworten (1)
Guillaume
am 18 Sep. 2014
find(V, 1);
find doesn't care that your vector is logical, it'll find the non-zero values anyway.
3 Kommentare
Velautham Daksiya
am 14 Okt. 2021
V1=find(V==a,b,'first')
a- integer to be found
b-no of entries to be found
'first' or 'last' - from first or from last of v
Siehe auch
Kategorien
Mehr zu Logical 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!