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

1 view (last 30 days)
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.

Answers (0)

Community Treasure Hunt

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

Start Hunting!