Detecting a Continuing Increase/Decrease, or Stabilizing Trend in Data

116 views (last 30 days)
Hi,
Jumping into the question: so I have some data in somewhat like this:
A = [1 2 1 2 1 2 3 4 5 6 7];
basically it will start to increase at some point, and I'd like to know where that 'some point' is. For the example above, is there a code/function that lets MatLab detect the increase in consecutively 3 numbers (6th element) and return the position?
The final purpose is to delete the parts before the consecutive increase and keep the parts after it; for now I'd really like to understand that part;
Thanks in advance,
  1 Comment
Image Analyst
Image Analyst on 28 Jun 2016
The signal starts increasing, with a run of 3 or more, at element #5, with the number 1. Why did you say it starts at element number 6?

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 28 Jun 2016
This code is fairly robust. It finds all runs where it's monotonically increasing for 3 elements or more and prints out information about that run. It can handle more than one run, or no runs at all, and gets info on all existing runs.
A = [1 2 1 2 1 2 3 4 5 6 7 5 8 0 1 3 6 9 4];
da = diff(A)>0
stats = regionprops(bwlabel(da), 'Area', 'PixelIdxList')
% Find which runs have a length of at least
% 3 elements of monotonically increasing elements
threeOrLonger = find([stats.Area] >= 3)
% Print out those sequences.
if ~isempty(threeOrLonger)
for blobIndex = 1 : length(threeOrLonger)
% This blob is 3. Find where the blob starts and stops.
startingIndex = stats(threeOrLonger(blobIndex)).PixelIdxList(1);
endingIndex = stats(threeOrLonger(blobIndex)).PixelIdxList(end)+1;
% Print out that run.
fprintf('For run #%d, startingIndex = %d, endingIndex = %d, the elements = ', ...
blobIndex, startingIndex, endingIndex);
fprintf('%d', A(startingIndex : endingIndex));
fprintf('\n');
end
else
msgbox('No run is 3 or longer');
end

Superficial
Superficial on 12 Mar 2020
I know I'm late to this thread, but this is my solution to this problem. Perhaps not as elegant but easy to understand.
I wanted to determine the value (in y) and timepoint (in x) of the start of a reaction.
for i=2:length(y)-1 % Exclude first and last values in series so y(i-1) and y(i+1) actually do exist
if y(i-1)<y(i) && y(i)<y(i+1) % finds 3 consecutive rising values
break
end % Ends the 'if' statement at this point.
end
% i is therefore the index of the (middle of 3) rising values in y
risevalue=y(i)
% In my case I wanted the x value as well to define the x (time) value at the same point
timepoint=x(i);

KSSV
KSSV on 28 Jun 2016
You can calculate diff of diff of A, when it remains constant, form that position the function increases. Eg:
A = [1 2 1 2 1 2 3 4 5 6 7];
K = diff(diff(A)) ;
idx = find(K==0) ;
iwant = idx(1)+1
Also go through monotonically increasing functions and monotonically decreasing functions.
Eg.
x = [1 2 3 4 5 6]
all(diff(x)>0) % increasing
% But now:
x(7) = 3
all(diff(x)>0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!