I have to obtain all possibile combination of this kind of matrix:
String[][] matrix = {
{"AA-123", "AA-124", "AA-125", "AA-126"},
{"BB-12", "BB-13"},
{"CC-1"},
};
After all, that is the final implementation. It's in Java, but the language may be irrilevant:
long nComb = 1;
for (int iMatr = 0; iMatr < matrix.length; iMatr++)
nComb *= matrix[iMatr].length;
for (int iComb = 0; iComb < nComb; iComb++) {
System.out.print("|");
long nSec = 1;
for (int iSec = 0; iSec < matrix.length; iSec++) {
String[] sec = matrix[iSec];
for (int iAtom = 0; iAtom < sec.length; iAtom++) {
if (iAtom == ((iComb / nSec) % sec.length))
System.out.print(1);
else
System.out.print(0);
}
nSec *= sec.length;
System.out.print("|");
}
System.out.println();
}
I have to apply my logic on the if
that it prints 1 or 0. I need to know what is the current element (index) of the combination of the array. The expected result:
|1000|10|1|
|0100|10|1|
|0010|10|1|
|0001|10|1|
|1000|01|1|
|0100|01|1|
|0010|01|1|
|0001|01|1|
Regards.
Edit:
I find a possible answer using another variable in the array iteration: nSec
. It product increse by the lenght of the array over iterations, reaching at the last iteration the value of nComb
.