how to select the best 15 scores

2 ビュー (過去 30 日間)
Sam
Sam 2015 年 11 月 6 日
編集済み: Kirby Fears 2015 年 11 月 9 日
Hi,
How can I select the * best* 15 scores of a 70 element vector with some NaN values in it (which should not be selected)?

採用された回答

Kirby Fears
Kirby Fears 2015 年 11 月 6 日
編集済み: Kirby Fears 2015 年 11 月 6 日
When you sort descending, the NaN values are actually sorted on top. You can use indexing to ignore NaN's before sorting. I'm adding a bit of extra code to keep track of the original position of these best 15 scores (you might want it later).
myData = rand(70,1); % pretend data
myData([1,2,4,6]) = NaN; % adding NaNs for testing
idxKeepers = find(~isnan(myData)); % index for non-NaN data
[~,idxSort] = sort(myData(idxKeepers),'descend');
idxBestScores = idxKeepers(idxSort(1:15));
bestScores = myData(idxBestScores);
idxBestScores contains the location of the best 15 scores in your original data. bestScores contains the 15 best scores (sorted descending).
  2 件のコメント
Sam
Sam 2015 年 11 月 7 日
編集済み: Sam 2015 年 11 月 7 日
Thanks! And how can i select the 15 worst scores for the same 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)
Kirby Fears
Kirby Fears 2015 年 11 月 9 日
編集済み: Kirby Fears 2015 年 11 月 9 日
Sam,
The idxSort array contains the ordered positions for scores from best to worst. Instead of taking the first 15 elements of idxSort, you can take the last 15 elements to get the worst 15 scores.
idxWorstScores = idxKeepers(idxSort(end-14:end));
You can loop across similar arrays such as TRIG, PAL, etc, by storing their data into one structure that you can index across. For example, you can store the initial scores into a struct like this:
MyData.TRIG.scores = num(:,strcmp('TRIG',txt(1,:)));
Repeat for PAL and others. Then you can store field names of MyData and loop over them.
fn = fieldnames(MyData);
for iter = 1:numel(fn),
disp(MyData.(fn{iter}).scores);
...
...
data.(fn{iter}).bestScores = ... ;
end
Please give this a try.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 11 月 6 日
Sort descending and take the first 15 entries of the result.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by