Finding Consecutive Numbers that Exceed a Threshold in MATLAB
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
Recent Comments
Archives
- April 2023
- January 2023
- November 2022
- May 2022
- March 2022
- January 2022
- December 2021
- April 2021
- December 2020
- October 2020
- August 2020
- July 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- January 2019
- December 2018
- November 2018
- August 2018
- July 2018
- April 2018
- March 2018
- November 2017
- October 2017
- February 2017
- October 2016
- August 2016
- July 2016
- November 2015
- October 2013
- February 2013
- January 2013
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- February 2012
- December 2011