# Detect monotonic decrease and record the corresponding rate

1 view (last 30 days)
Joy on 26 Mar 2024
Edited: Joy on 27 Mar 2024
Assume I have the following data:
D1= {'3/25/2024 15:01:10' 15
'3/25/2024 15:01:26' 25
'3/25/2024 15:01:42' 25
'3/25/2024 15:01:58' 23
'3/25/2024 15:02:14' 22
'3/25/2024 15:02:30' 15
'3/25/2024 15:02:46' 25
'3/25/2024 15:02:50' 24
'3/25/2024 15:02:59' 23}
I'd like to find the rate the variable decreases from 25 to 15. For this dataset, once the value has reached 15, it will jump back up to 25, and I'd like to record the next decreasing rate (and repeat).
So for example, I would record 10/ ('3/25/2024 15:02:46' - '3/25/2024 15:02:30') or 10/(16 seconds)
The next rate would be 10 / ('3/25/2024 15:02:59' - '3/25/2024 15:02:50') or 10/(9 seconds).
I believe I need to use ischange to detect when the values jump back up to 25, and ignore noise. (Assuming that the decreasing rate is monotonic).
Thanks
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Les Beckham on 26 Mar 2024
Your "for example" text doesn't seem to match with finding how long it takes to decrease from 25 to 15.
So for example, I would record 10/ ('3/25/2024 15:02:46' - '3/25/2024 15:02:30') or 10/(16 seconds)
The next rate would be 10 / ('3/25/2024 15:02:59' - '3/25/2024 15:02:50') or 10/(9 seconds).
Your first example points are for an increase from 15 to 25 and your second example is for an decrease from 24 to 23.
If you really are wanting to find the average slopes of the decreases from 25 to 15. Try this approach (as a starting point at least). Note that I added an extra point to your data so there would be more than one decrease from 25 to 15.
D1= {'3/25/2024 15:01:10' 15
'3/25/2024 15:01:26' 25
'3/25/2024 15:01:42' 25
'3/25/2024 15:01:58' 23
'3/25/2024 15:02:14' 22
'3/25/2024 15:02:30' 15
'3/25/2024 15:02:46' 25
'3/25/2024 15:02:50' 24
'3/25/2024 15:02:59' 23
'3/25/2024 15:03:05' 15}; % << added a data point so there are two 25 to 15 transitions
T = cell2table(D1, 'VariableNames', {'timestamp', 'data'});
T.timestamp = datetime(T.timestamp(:))
T = 10x2 table
timestamp data ____________________ ____ 25-Mar-2024 15:01:10 15 25-Mar-2024 15:01:26 25 25-Mar-2024 15:01:42 25 25-Mar-2024 15:01:58 23 25-Mar-2024 15:02:14 22 25-Mar-2024 15:02:30 15 25-Mar-2024 15:02:46 25 25-Mar-2024 15:02:50 24 25-Mar-2024 15:02:59 23 25-Mar-2024 15:03:05 15
Since we only care about the cases where the data decreases from 25 to 15, eliminate any data that is not 25 or 15 and also eliminate any data before the first occurrence of 25.
idx = T.data == 25 | T.data == 15;
T = T(idx,:);
if T.data(1) ~= 25
T(1,:) = []
end
T = 5x2 table
timestamp data ____________________ ____ 25-Mar-2024 15:01:26 25 25-Mar-2024 15:01:42 25 25-Mar-2024 15:02:30 15 25-Mar-2024 15:02:46 25 25-Mar-2024 15:03:05 15
slope = diff(T.data) ./ seconds(diff(T.timestamp));
We only care about negative slopes so throw away any non-negative slopes.
slope = slope(slope < 0) % slopes in data units per second
slope = 2x1
-0.2083 -0.5263
##### 3 CommentsShow 1 older commentHide 1 older comment
Les Beckham on 27 Mar 2024
You are quite welcome. If your problem is solved, please Accept the answer that best helped you solve it. If you have additional specific questions, please provide the details or additional data.
Joy on 27 Mar 2024
Edited: Joy on 27 Mar 2024
I'm concerned about the noise in my dataset. I'm trying to use ischange to detect when there are large changes. Now i'm trying to find each local maximum and the sequential minimum and taking the rate. Do you have any advice on this? Thanks

Sign in to comment.

### More Answers (1)

Image Analyst on 26 Mar 2024
Does this help?
D1= {'3/25/2024 15:01:10' 15
'3/25/2024 15:01:26' 25
'3/25/2024 15:01:42' 25
'3/25/2024 15:01:58' 23
'3/25/2024 15:02:14' 22
'3/25/2024 15:02:30' 15
'3/25/2024 15:02:46' 25
'3/25/2024 15:02:50' 24
'3/25/2024 15:02:59' 23};
t = cell2table(D1)
t = 9x2 table
D11 D12 ______________________ ___ {'3/25/2024 15:01:10'} 15 {'3/25/2024 15:01:26'} 25 {'3/25/2024 15:01:42'} 25 {'3/25/2024 15:01:58'} 23 {'3/25/2024 15:02:14'} 22 {'3/25/2024 15:02:30'} 15 {'3/25/2024 15:02:46'} 25 {'3/25/2024 15:02:50'} 24 {'3/25/2024 15:02:59'} 23
var = t{:, 2};
rows15 = find(var == 15)
rows15 = 2x1
1 6
dateTimes = D1(rows15, 1)
dateTimes = 2x1 cell array
{'3/25/2024 15:01:10'} {'3/25/2024 15:02:30'}
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!