Finding Consecutive Numbers that Exceed a Threshold in MATLAB

p-value-thresh.png

The goal is to determine if there exists a span of n p-values that exceed threshold t

pVals = [0.91 0.66 0.23 0.96 0.99 0.97 0.99 0.83 0.12 0.96 0.99 0.97 0.76];

n = 3;

t = 0.95;

The total amount of values exceeding the threshold can be found simply:

sum(pVals > t);

However, we are also requiring that those p-values be consecutive. We can using a moving sum centered on the current element plus n on the binary index of p-value threshold crossings to do this:

ntpIdx = movsum(pVals > t,[0 n-1]) == n;

ntpIdx =

  1×13 logical array

   0   0   0   1   1   0   0   0   0   1   0   0   0

If you only want the initial crossing indexes:

ntpIdx_init = logical(diff([0 ntpIdx]) == 1);

ntpIdx_init =

  1×13 logical array

   0   0   0   1   0   0   0   0   0   1   0   0   0