Find data between min and max

조회 수: 8 (최근 30일)
Ahmed Alalawi
Ahmed Alalawi 2020년 1월 30일
댓글: Star Strider 2020년 1월 30일
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
plot(time_s,task,time_s(TFmax),task(TFmax),'b*')
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
Star Strider 2020년 1월 30일
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);
figure
plot(time_s,task, time_s (TFmin),task(TFmin),'r*')
hold on
plot(time_s,task,time_s(TFmax),task(TFmax),'b*')
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’.
  댓글 수: 2
Ahmed Alalawi
Ahmed Alalawi 2020년 1월 30일
Amazing !
Star Strider
Star Strider 2020년 1월 30일
Thank you!

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

추가 답변 (1개)

Jeremy
Jeremy 2020년 1월 30일
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)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by