MATLAB: Find highest third of values in dateset

highest thirdsignificant wave height

WaveHeight=[3 1 5 2 3.1 6 5.5 3 2.5];
We need to find the highest 1/3:
HighestThird=[6 5.5 5]
But what if we what a function that will calculate the mean of the heights 1/3 of WaveHeight with the length of WaveHeight being a variable which may be large?
E.g.
WaveHeight=randi(1000,1,1000);
HighestThird=

Best Answer

One approach is to use the sort function:
WaveHeight=[3 1 5 2 3.1 6 5.5 3 2.5];
[Waves,Idx] = sort(WaveHeight, 'descend');
HighestThird = WaveHeight(Idx(1:fix(numel(Idx)/3)))
HighestThird =
6 5.5 5
also:
HighestThird = Waves(1:fix(numel(Idx)/3))
with the same result.