# Find data between min and max

6 views (last 30 days)
Ahmed Alalawi on 30 Jan 2020
Commented: Star Strider on 30 Jan 2020
Hello there,
I have used the two functions islocalmin and islocalmax to find minimum and maximum points in my data.
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

Star Strider on 30 Jan 2020
An accumarray call can do that relatively easily:
time_s = D.time_s;
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);
figure
hold on
plot(X1, Y1, '.r') % Plot First Set
plot(X3, Y3, '.g') % Plot Third Set
hold off
Producing this example plot:
Thius code recovers all of the intervals, not only the ascending ones, so choose the odd-numbered arguments to ‘GetData’.
##### 2 CommentsShowHide 1 older comment
Star Strider on 30 Jan 2020
Thank you!

Jeremy on 30 Jan 2020
Something like this?
t = linspace(0,2*pi,201);
y = sin(t);
plot(t,y,'LineWidth',2)
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;
end
t_int = t(data_id);
y_int = y(data_id);
plot(t_int,y_int,'r--','LineWidth',2)