MATLAB: How to find the first instance of consecutive numbers above a threshold in an array

arrayconsecutive values

I have this code which converts the output of a process i'm running into a table, then to an array, and then finds the first instance that a number crosses a threshold:
T = table(Spots); %convert output of Ornstein-Uhlenbeck process to table
A = table2array(T); %convert table to array
>> [I,J] = find(A<-400); %find first instance of -400 in each column, and report the row position
>> [~,m] = unique(J, 'first');
>> I(m);
The array is 300×4 double and I and J are both 797×1 double.
I am trying to change this to find the first instance of two consecutive numbers below the threshold (-400) and have that reported to me. What would be the best way to go about this?
Thanks,
Mike

Best Answer

You can use pretty much the same code that you were using. Just change the find condition:
[row, col] = find(A(1:end-1, :) < -400 & A(2:end, :) < -400)
[~, idx] = unique(col, 'first');
rows = row(idx)
A more concise and faster, but more obscure way to do the above three lines is to use max on the logical array:
[~, rows] = max(A(1:end-1, :) < -400 & A(2:end, :) < -400)