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

조회 수: 14 (최근 30일)
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
Image Analyst
Image Analyst 2015년 7월 24일
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 2015년 7월 24일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2015년 7월 24일
편집: Guillaume 2015년 7월 24일
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)

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 7월 24일
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 2015년 7월 23일
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
Michael Suffield
Michael Suffield 2015년 7월 24일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by