How to construct a combinatorics array from two data sets with each combination having a specified number of elements?

Hi I have two data sets. The first one consists of N1(let's say, 10) elements and the second one has N2(i.e., 8) elements. All the elements of the smaller set has its counterpart in the larger set. I need to construct a combinatorics array that will consist of all the possible combinations of the two data sets with each combinations having N1 elements. However, an element from a data set cannot co-exist with its counter-part from the other data-set.
For example, Set1 = [F1 F2 F3] and set2 = [S2 S3] ; (here, N1 = 3, N2 =2) Now the desired set will be: M = [F1 F2 F3; F1 S2 F3; F1 F2 S3; F1 S2 S3]
So, the total number of possible combinations is : 2^N2. I want Matlab to construct the 'M' set. Any suggestions/insight to this problem will be highly appreciated. Thanks

 Respuesta aceptada

E.g., but of course only makes sense for small sizes of your variables
Set1 = [1,2,3];
Set2 = [4 5];
columns = [2 3]; % the columns in Set1 corresponding to Set2
N = 2^numel(Set2); % number of combinations
b2 = logical(dec2bin(0:(N-1))-'0'); % logical locations of the replacements in Set2
b1 = false(N,numel(Set1)); % logical locations of the replacements in Set1
b1(:,columns) = b2; % fill appropriate columns of b1 with b2
result = repmat(Set1,N,1); % result is Set1 by default
Set2rep = repmat(Set2,N,1); % get the replacement values
result(b1) = Set2rep(b2); % replace the appropriate values
The result:
>> result
result =
1 2 3
1 2 5
1 4 3
1 4 5

Más respuestas (1)

Sad, people keep asking unreasonable thing. The number of combinations is
2^80,
Each of those is 100 numbers so the size in byte is
>> 2^80*100*8
ans =
9.6714e+26 bytes
>> (2^80*100*8)/(1024^4)
ans =
8.7961e+14 Tera bytes
You can take all the computers on earth, it'ss still impossible to store such quantity, let alone compute it.

2 comentarios

MRR
MRR el 6 de Nov. de 2018
Editada: MRR el 6 de Nov. de 2018
Thanks for your kind reply. While the 80 and 100 are just numbers as an example, N1 and N2 are clearly noted in the question that can take any value. Further, in the illustration, N1 and N2 are only 3 and 2 respectively. While the number is arbitrary (be it 80 or 8 or 80000!), the algorithm is not, right? Thanks for your effort though in answering the question! However, the numbers in the questions are modified so that no one get distracted by something that is necessarily not part of the problem.
The point is however, that what you really want to solve is a problem of that size. For 2 or 3 levels, you would never have bothered to ask the question in the first place.
So IF you cannot solve the problem you think that you want to solve, that means you need to focus on how to make the problem manageable, thus using a completely different fundamental algorithm.

Iniciar sesión para comentar.

Categorías

Más información sobre Elementary Math en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

MRR
el 5 de Nov. de 2018

Editada:

MRR
el 6 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by