How can I efficiently select the best N items from a vector in different groups?

1 次查看(过去 30 天)
I need to select the best (weighted) items from several groups (days), like this
days = [737399 737399 737399 737399 737400 737400 737400];
weights = [0.2 0.6 0.8 0.5 0.5 0.2 0.6];
numBestItemsEachDay = 2;
selectedItems = true(size(weights));
for today = unique(days),
itemsToday = find(days == today);
if (length(itemsToday)>numBestItemsEachDay),
[~,selectedItemsToday] = sort(weights(itemsToday),'descend');
selectedItems(itemsToday(selectedItemsToday((numBestItemsEachDay+1):end))) = false;
end;
end;
selectedItems
The idea is to perform this operation more efficiently, avoiding loops.
I am using R2016b, so I can not use maxk. Also, splitapply returns an array of cells, so I have to implement a for loop to rearrange it.

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by