Search code examples
matlabsortingindexingfindminimum

How to find the minimum 10th percent of data in Matlab?


I have a vector [224x1] of data and another matching vector of time [224x1]. I want to find the minimum values occupying the lowest 10 percent of the data. How can I do that and find the matching times?

I tried using the sort function (from here: https://www.mathworks.com/matlabcentral/answers/249619-how-do-i-get-the-top-10-percent-of-the-matrix-value) but the times are no longer in chronological order. This is the code I used:

[sortedValues,sortIndex] = sort(THUL_ST_JJA(:),'ascend'); %Sort Ascending
[sortedDTValues,sortDTIndex] = sort(THUL_dt_JJA(:),'ascend'); %Sort Ascending

idx = sortIndex(1:ceil(length(sortIndex)*0.1)); %Minimum 10th percent STs
THUL_ST_min = THUL_ST_JJA(idx);
THUL_dt_min = THUL_dt_JJA(idx); %Find matching 10th percent minimum times 

Here's a sample of time (mm/dd/yyyy hh:mm:ss) and temperature out of order:

06/08/2012 07:00:00 -5.26
06/08/2012 10:00:00 -5.18
06/08/2012 09:00:00 -5.1
06/08/2012 08:00:00 -5.07
06/08/2012 06:00:00 -4.84
06/08/2012 11:00:00 -4.84
06/09/2012 06:00:00 -4.84
06/09/2012 07:00:00 -4.82
06/09/2012 05:00:00 -4.79
06/09/2012 08:00:00 -4.65
08/29/2012 05:00:00 -4.61
06/09/2012 09:00:00 -4.49
08/29/2012 06:00:00 -4.47

Solution

  • Sort the data. Then sort the time using the same index. This keeps the pairs together.

    Then get your index to the data you want & apply the same index to the time.

    [sortData, sortIDX] = sort(rawData,'ascend');
    sortTime            = rawTime(sortIDX);
    
    bottom10IDX = 1:ceil(length(sortData)*0.1);
    bottom10Data = sortData(bottom10IDX);
    bottom10Time = sortTime(bottom10IDX);