Azzera filtri
Azzera filtri

creating array with greatest variety of numbers

1 visualizzazione (ultimi 30 giorni)
Austin Welch
Austin Welch il 9 Lug 2018
Riaperto: Walter Roberson il 22 Dic 2018
I am trying to create an array that contains the most variety of numbers. So, I am doing an n choose k of 22 and 6 respectively. so I have around 72,000 different combinations of 6 numbers from 1-22. Now, I am trying to select an arbitrary number of these combinations to use later on in my code, but that is not really related. My problem is that when I am selecting these combinations I want to select the combinations that use the same numbers the least. For example, if I ask for 4 combinations I don't want it to give me
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
1 2 3 4 5 9
as the combinations I would more so like something that uses the most of the 22 numbers available and also when it does use a same number in multiple combinations, it puts the number with different numbers than it was placed with the first time so there is more variety. For example this would be different numbers but not much different combos when used multiple times:
1 2 3 4 5 6
7 8 9 10 11 12
1 2 3 4 5 7
13 14 15 16 17 18
19 20 21 22 13 12
So this uses all the different numbers, but when it uses 1 again it puts it with the same numbers as before.
This would be more desirable because it puts 1 with different numbers than before:
1 2 3 4 5 6
7 8 9 10 11 12
1 9 7 14 17 18
13 14 15 16 17 18
19 20 21 22 13 12
Now again, I already have it generating all the combinations and I'm just figuring how to select them. I am sorry if this is confusing, I have to admit I can not put it into words very effectively because I am still not entirely sure what is possible and what I am really looking for. Thanks for help if anyone has any ideas.

Risposta accettata

Jan
Jan il 9 Lug 2018
Modificato: Jan il 9 Lug 2018
If you want num vectors with a great "variability" taken from the set of permutations with n elements taken from 1:k:
k = 22;
n = 6;
num = 5;
pool = repmat(1:k, 1, ceil(n * num / k)); % At least |n * num| elements
ready = false;
limit = 1e7;
while ~ready && limit > 0
Result = reshape(pool(randperm(length(pool), n * num)), num, n);
Test = unique(sort(Result, 2), 'rows');
ready = (size(Test, 1) == num) && all(all(diff(Test, 1, 2)));
limit = limit - 1;
end
if limit == 0
error('Cannot find valid set of permutations.')
end
This is a rejection method, which selects a random permutation from a vector created by concatenations of [1:k, 1:k, ...]. Here 1:k is repeated the minimal number of times, such that you can pick n * num vectors from it with an equal chance for all elements. The chance to find a valid set is higher, if k is larger and num is smaller.
If the method fails, you can use a fallback, which creates at least a valid set without considering the "variability".

Più risposte (0)

Prodotti


Release

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by