I have plotted a graph of (y,t). I need to get 5 minimum and 5 maximum values with the corresponding Y values. My code below is just for one min and max value of T.

2 views (last 30 days)
min_T= min(T)
max_T=max(T)
index = find(T == min_T);
YAtMin = Y(index)

Accepted Answer

Image Analyst
Image Analyst on 14 Oct 2014
Just sort the data and take the first 5 and last 5 sorted elements:
clc;
T = randi(99, 1, 11) % Generate sample data.
[sortedT, sortOrder] = sort(T, 'ascend')
% Extract the mins and maxes:
theMins = sortedT(1:5)
theMinIndexes = sortOrder(1:5)
% Determine their indexes in the original array.
theMaxes = sortedT(end-4:end)
theMaxIndexes = sortOrder(end-4:end)
In the command window:
T =
85 93 68 76 74 39 65 17 70 4 28
sortedT =
4 17 28 39 65 68 70 74 76 85 93
sortOrder =
10 8 11 6 7 3 9 5 4 1 2
theMins =
4 17 28 39 65
theMinIndexes =
10 8 11 6 7
theMaxes =
70 74 76 85 93
theMaxIndexes =
9 5 4 1 2

More Answers (0)

Community Treasure Hunt

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

Start Hunting!