MATLAB Answers

Find data between min and max

6 views (last 30 days)
Hello there,
I have used the two functions islocalmin and islocalmax to find minimum and maximum points in my data.
TFmin = islocalmin(task, 'MinProminence',10)
TFmax = islocalmax(task, 'MinProminence',10)
plot(time_s,task, time_s (TFmin),task(TFmin),'r*')
hold on
I have identified different points as you can see in the attached plots, and I ended up with 10 points.
Now, I need to create two variables (task + time_s) repressing the data that lies in between each minimum and maximum point.
For example (see attached pic):
I need extract the exact date of time and task between the points:
1 and 2
3 and 4
5 and 6
7 and 8
9 and 10
And also the opposite:
2 and 3
4 and 5
6 and 7
8 and 9
Any advice or help would be much appreciated.
Thank you

Accepted Answer

Star Strider
Star Strider on 30 Jan 2020
An accumarray call can do that relatively easily:
D = load('test.mat');
task = D.task;
time_s = D.time_s;
TFmin = islocalmin(task, 'MinProminence',10);
TFmax = islocalmax(task, 'MinProminence',10);
MinIdx = find(TFmin);
MaxIdx = find(TFmax);
Idx = sort([MinIdx; MaxIdx]);
IdxCell = accumarray(Idx(1:numel(Idx)-1), (1:numel(Idx)-1).', [], @(x){Idx(x):Idx(x+1)}); % Cell Array Of Index Ranges
IdxRng = find(cellfun(@(x)~isempty(x), IdxCell)); % Index Ranges
GetData = @(x) [time_s(IdxCell{IdxRng(x)}), task(IdxCell{IdxRng(x)})]; % Function To Retrieve Data Easily
Get1 = GetData(1); % Get First Set
X1 = Get1(:,1);
Y1 = Get1(:,2);
Get3 = GetData(3); % Get Third Set
X3 = Get3(:,1);
Y3 = Get3(:,2);
plot(time_s,task, time_s (TFmin),task(TFmin),'r*')
hold on
plot(X1, Y1, '.r') % Plot First Set
plot(X3, Y3, '.g') % Plot Third Set
hold off
Producing this example plot:
Find data between min and max - 2020 01 30.png
Thius code recovers all of the intervals, not only the ascending ones, so choose the odd-numbered arguments to ‘GetData’.

More Answers (1)

Jeremy on 30 Jan 2020
Something like this?
t = linspace(0,2*pi,201);
y = sin(t);
grid on
hold on
minid = find(islocalmin(y));
maxid = find(islocalmax(y));
[~,d] = min([minid maxid])
switch d
case 1
data_id = minid:1:maxid;
case 2
data_id = maxid:1:minid;
t_int = t(data_id);
y_int = y(data_id);



Community Treasure Hunt

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

Start Hunting!