arrays - Find indices of blocks of 0s that are continuous -
this question has answer here:
- finding islands of zeros in sequence 7 answers
i have vector , want find indices of blocks of 0s continuous @ least 3 times.
y = [1 1 1 0 1 1 0 0 0 1 1 1 0 1 0 1 0 0 1 0 0 0 0 1 1];
so in case, blocks should [0 0 0] 7-9 , [0 0 0 0] 20-23. output should give me indices, [7, 9] , [20,23], or better, change these blocks of 0s single nan become:
[1 1 1 0 1 1 nan 1 1 1 0 1 0 1 0 0 1 nan 1 1]
thanks!
what can is:
- pad vector
1
on each side. - use
find
,diff
find vector changes 1 0 (diff = -1) - use
find
,diff
find vector changes 0 1 (diff = 1) - find duration of each interval subtracting values in 3 values in 2 (and add 1)
- create logical vector
true
duration>= 3
, , use vector find start indices (from values found in point 2). - set value of each of start indices
nan
- set value of
start indices + 1 : end indices
[]
.
and you're set go!
it took lot more time writing explanation took write code. it's quite nice exercise learn basic matlab i'll leave you. luck!
Comments
Post a Comment