get the index of the n one in a binary vector

18 views (last 30 days)
Dear,
I have binary vector like bv=[0,0,0,0,1,0,0,1,1,1,0,0,1,0,1...] and I would like to get the index in bv of the n "1"
For example the index of the 4th "1" in bv is 10.
I know that using while loops and two counters it possible but I asking for are vectorized (more Matlab) solution
Thanks in advance,

Answers (1)

Stephen23
Stephen23 on 21 Jan 2019
>> bv=[0,0,0,0,1,0,0,1,1,1,0,0,1,0,1];
>> idx = find(bv);
>> idx(4)
ans = 10

Tags

Community Treasure Hunt

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

Start Hunting!