Find minimum or maximum value - two conditions

13 views (last 30 days)
LukeJes
LukeJes on 9 May 2023
Commented: LukeJes on 10 May 2023
Hi there,
I have a problem where I am trying to find the index of minimum or maximum values based on two conditions. I have separated my data into 10 second blocks, with 1 data point per second. The two conditions are as follows:
1) the next minimum value must not be within 5 seconds of the previous minimum value
2) the next minimum value must be within the 10 second block following the previous minimum value's 10 second block.
Presently, I can ensure the next indexed minimum value meets one condition or the other, but I cannot ensure that it meets both.
Any ideas would be much appreciated!
Luke
  3 Comments
LukeJes
LukeJes on 9 May 2023
Edited: LukeJes on 9 May 2023
Please see attached.
I've included my code below, up until where I am having the problem.
My expected result would be a minimum value from each 10 second block (odds:evens) that are each not within 5 seconds of their previous minimum value. Ideally it'd be a find function with two conditions, such as find(min(data(idxmcamin(j-1)+5:evens(j),2)) && min(data(odds(j-1):evens(j),2)). I hope this makes sense!
clear
spreadsheets = dir('*.xlsx');
spreadsheets = {spreadsheets.name};
for i = 1:length(spreadsheets)
trial = spreadsheets(i);
trial = char(trial);
data = xlsread(trial);
evens = 0:10:length(data);
evens = evens(1,2:end);
odds = 1:10:length(data);
odds = odds(1,2:end);
mcaoutliers = isoutlier(data(:,2));
mapoutliers = isoutlier(data(:,3));
if sum(mcaoutliers)>0
data(:,2) = filloutliers(data(:,2),"linear");
elseif sum(mapoutliers)>0
data(:,3) = filloutliers(data(:,3),"linear");
else
%
end
% find max and min and time
for j = 1:length(evens)
[idxevens,~] = find(data(:,1)==evens(j));
if j == 1
% mca
valmcamin(j) = min(data(2:idxevens,2));
valmcamax(j) = max(data(2:idxevens,2));
[idxmcamin(j),~] = find(data(:,2)==valmcamin(j),1,'first');
[idxmcamax(j),~] = find(data(:,2)==valmcamax(j),1,'first');
timemcamin(j) = data(idxmcamin,1);
timemcamax(j) = data(idxmcamax,1);
% map
valmapmin(j) = min(data(2:idxevens,3));
valmapmax(j) = max(data(2:idxevens,3));
[idxmapmin(j),~] = find(data(:,3)==valmapmin(j),1,'first');
[idxmapmax(j),~] = find(data(:,3)==valmapmax(j),1,'first');
timemapmin(j) = data(idxmapmin,1);
timemapmax(j) = data(idxmapmax,1);
else
% mca
valmcamin(j) = min(data(idxmcamin(j-1)+5:evens(j),2)); % PROBLEM
valmcamax(j) = max(data(idxmcamax(j-1)+5:evens(j),2)); % PROBLEM
[idxmcamin(j),~] = find(data(:,2)==valmcamin(j),1,'first');
[idxmcamax(j),~] = find(data(:,2)==valmcamax(j),1,'first');
timemcamin(j) = data(idxmcamin(j),1);
timemcamax(j) = data(idxmcamax(j),1);
LukeJes
LukeJes on 10 May 2023
Please let me know if you require any other information to help with answering this question :)

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 9 May 2023
I think the islocalmin and islocalmax functions will be of use to you. See the MinSeparation name-value argument.
  2 Comments
LukeJes
LukeJes on 9 May 2023
Edited: LukeJes on 9 May 2023
Hi Steven,
Thanks for the suggestion. Unfortunately islocalmin with the MinSeparation argument is a bit off in places. I've attached my data and code above to give you more idea of what I'm working with :)
LukeJes
LukeJes on 10 May 2023
Please let me know if you require any other information to help with answering this question :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!