stepwise sort combvec result by its sum over one dimension
Take this code, which works for small lists:
% generate a small list (only works for small randi)
list = cell(1,7);
for idx = 1:7
list{idx} = randi(100,1,randi(10,1));
end
%generate all possible combinations and sort them based on their sum
all_combinations = combvec(list{:});
[~, sorted_idx] = sort(sum(all_combinations, 1),’descend’);
all_combinations_sorted = all_combinations(:,sorted_idx);
%do some work with the generated list
for idx = 1:size(all_combinations_sorted, 2)
combination = all_combinations_sorted(:,idx);
% do more stuff
if some_criteria
break;
end
end
So first I am generating all possible combinations of elements from the 7 lists. Then I calculate the sum of each generated element, e.g. the combination a = [1; 1; 1; 1; 1; 1; 1] has sum(a) == 7. Based on this sum I sort all combinations. Afterwards I iterate over this sorted list and do my work until a stopping criteria is met.
The problem is that, contrary to my example the lists I am dealing with are in the range 100 < size(list{idx}) < 5000 . Thus there are to many combinations to generate all of them, let alone sorting the result. I usually don’t need to go through the entire list, but rather the first 1000 or so entries. My question is:
How can I generate this *sorted* list sequentially, so that I start with the biggest element and then gradually step down to the smallest?Take this code, which works for small lists:
% generate a small list (only works for small randi)
list = cell(1,7);
for idx = 1:7
list{idx} = randi(100,1,randi(10,1));
end
%generate all possible combinations and sort them based on their sum
all_combinations = combvec(list{:});
[~, sorted_idx] = sort(sum(all_combinations, 1),’descend’);
all_combinations_sorted = all_combinations(:,sorted_idx);
%do some work with the generated list
for idx = 1:size(all_combinations_sorted, 2)
combination = all_combinations_sorted(:,idx);
% do more stuff
if some_criteria
break;
end
end
So first I am generating all possible combinations of elements from the 7 lists. Then I calculate the sum of each generated element, e.g. the combination a = [1; 1; 1; 1; 1; 1; 1] has sum(a) == 7. Based on this sum I sort all combinations. Afterwards I iterate over this sorted list and do my work until a stopping criteria is met.
The problem is that, contrary to my example the lists I am dealing with are in the range 100 < size(list{idx}) < 5000 . Thus there are to many combinations to generate all of them, let alone sorting the result. I usually don’t need to go through the entire list, but rather the first 1000 or so entries. My question is:
How can I generate this *sorted* list sequentially, so that I start with the biggest element and then gradually step down to the smallest? Take this code, which works for small lists:
% generate a small list (only works for small randi)
list = cell(1,7);
for idx = 1:7
list{idx} = randi(100,1,randi(10,1));
end
%generate all possible combinations and sort them based on their sum
all_combinations = combvec(list{:});
[~, sorted_idx] = sort(sum(all_combinations, 1),’descend’);
all_combinations_sorted = all_combinations(:,sorted_idx);
%do some work with the generated list
for idx = 1:size(all_combinations_sorted, 2)
combination = all_combinations_sorted(:,idx);
% do more stuff
if some_criteria
break;
end
end
So first I am generating all possible combinations of elements from the 7 lists. Then I calculate the sum of each generated element, e.g. the combination a = [1; 1; 1; 1; 1; 1; 1] has sum(a) == 7. Based on this sum I sort all combinations. Afterwards I iterate over this sorted list and do my work until a stopping criteria is met.
The problem is that, contrary to my example the lists I am dealing with are in the range 100 < size(list{idx}) < 5000 . Thus there are to many combinations to generate all of them, let alone sorting the result. I usually don’t need to go through the entire list, but rather the first 1000 or so entries. My question is:
How can I generate this *sorted* list sequentially, so that I start with the biggest element and then gradually step down to the smallest? sort MATLAB Answers — New Questions