How do I find the first instance of consecutive numbers above a threshold in an array?

10 views (last 30 days)
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 300x4 double and I and J are both 797x1 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
  2 Comments
Image Analyst
Image Analyst on 24 Jul 2015
Are they all integers? So do you mean like -402 and -401? What does consecutive mean? Separated by exactly 1? What about -514.5 and -513.5? Have you tried the diff() function???
Michael Suffield
Michael Suffield on 24 Jul 2015
Sorry, should have been clearer.
Consecutive in this case means two numbers next to each other in the array which are both beyond the threshold, so in this case it could be either -401 and -402, or -401 and -407, or -410 and -402, etc, As long as both numbers are beyond the threshold. All numbers in the array are integers.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 24 Jul 2015
Edited: Guillaume on 24 Jul 2015
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)

More Answers (2)

Image Analyst
Image Analyst on 24 Jul 2015
If the numbers are integers, this works:
% Create sample data.
A = randi([-444,-430], 10, 10)
% Find the differences from one row to the row below it.
da = diff(A)
% Find which of those have a difference of exactly 1.
consecutive = da == 1
% Go across each column, finding the first consecutive pair
[rows, columns] = size(A);
% Instantiate array to hold our answers
theRows = -1 * ones(rows, 1);
for col = 1 : columns
thisColumn = da(:, col);
% Find the first 1
firstRow = find(thisColumn == 1, 1, 'first');
if ~isempty(firstRow)
fprintf('In column %d, rows %d and %d are consecutive with values of %d and %d.\n', ...
col, firstRow, firstRow+1, A(firstRow, col), A(firstRow+1, col));
theRows(col) = firstRow; % Save the value
else
% No consecutive numbers found
fprintf('No consecutive numbers are in column %d.\n', col);
end
end
% Print to command window:
theRows
Give it a try and see. It's well commented and explicit so it should be easy to follow.

Jon
Jon on 23 Jul 2015
Here's one of many ways, if I understand correctly your question:
thresh = 300;
blah = [100 200 300 400 500 600 700 600 500 400 300 200];
blah2 = blah>thresh;
ur_idx = find(blah2(1:end-1)+blah2(2:end)==2,1,'first')
  1 Comment
Michael Suffield
Michael Suffield on 24 Jul 2015
Thanks, this is working to a degree. My array has four columns, but the 'ur_idx = find...' part of that code is only reporting for the first column in the array. there are instances where there are consecutive numbers in columns 2, 3, and 4, how can i adjust the code to find those instances too?
Thanks

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!