How can I select the 15 best and worst score in a data set with NaN-values in it?
조회 수: 1 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
답변 (2개)
Guillaume
2015년 11월 9일
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?
댓글 수: 0
Thorsten
2015년 11월 9일
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!