Clear Filters
Clear Filters

How can I select the 15 best and worst score in a data set with NaN-values in it?

1 view (last 30 days)
How can i select the 15 best and worst scores for the data set? And how can I create a for-loop or something so that it calcultes all the best and worst 15 scores? For example: now i calculate the 15 best scores for PAL_12. How can i calculate it for PAL en TRIG too in a for-loop?
[num, txt, raw] = xlsread('data_copy');
TRIG = num(:,strcmp('TRIG',txt(1,:)));
PAL = num(:,strcmp('PAL',txt(1,:)));
PAL_12 = num(:,strcmp('PAL_12',txt(1,:)));
idxKeepers = find(~isnan(PAL_12)); % index for non-NaN data
[~,idxSort] = sort(PAL_12(idxKeepers),'descend');
idxBestScores = idxKeepers(idxSort(1:15)); %contains location of 15 best scores in original data
bestScores = PAL_12(idxBestScores); % contains the 15 best scores (sorted descending)

Answers (2)

Guillaume
Guillaume on 9 Nov 2015
I'm utterly confused. You've obviously managed to write some efficient code to do exactly what you want for PAL_12. Why can't you do the same for PAL and TRIG? And why do you want to use a loop when there's nothing to loop over?

Thorsten
Thorsten on 9 Nov 2015
You can store the labels in a cell string and then loop over this cell string.
labels = {'TRIG', 'PAL', 'PAL_12'}
for i = 1:numel(labels)
data = num(:,strcmp(labels{i},txt(1,:)));
idxKeepers = find(~isnan(data)); % index for non-NaN data
[~,idxSort] = sort(data(idxKeepers),'descend');
idxBestScores = idxKeepers(idxSort(1:15)); %contains location of 15 best scores in original data
bestScores(i,1:15) = data(idxBestScores);
idxWorstScores = idxKeepers(idxSort(end-14:end)); %contains location of 15 worst scores in original data
bestScores(i,1:15) = data(idxWorstScores);
end

Tags

Community Treasure Hunt

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

Start Hunting!