find the indices true values which were preceded by 10 false values in an array in matlab

You can use logical indexing along with diff function to find true values which were preceded by ten consecutive false values in an array as follows:

main.m
% example array
arr = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1];

% find logical indices for true values
idx_true = arr == 1;

% find logical indices for 10 consecutive false values
idx_false = diff([false, arr == 0]) == 1 & ...
            diff([arr == 0, false]) == -1 & ...
            [false, false, false, false, false, ...
             false, false, false, false, false, ...
             true(1, numel(arr)-10)];

% combine the two to find indices for true values which were 
% preceded by 10 false values
indices = find(idx_true & idx_false)
543 chars
17 lines

In the above code, idx_true finds logical indices for true values in the array. idx_false finds logical indices for false values which were preceded by ten consecutive false values. Finally, indices is an array of indices for true values which were preceded by ten consecutive false values.

gistlibby LogSnag