Just sort the data and take the first 5 and last 5 sorted elements:
clc;
T = randi(99, 1, 11)
[sortedT, sortOrder] = sort(T, 'ascend')
theMins = sortedT(1:5)
theMinIndexes = sortOrder(1:5)
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
Best Answer