Reputation: 843
Step one: I would like to return all possible outcomes of a 3x6 matrix where possible values is either 0 or 1.
For example: Possibility one:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Possibility Two:
0 0 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
etc...
Step two: I would like to give each position a probability of the outcome being 1 or 0 and therefore be able to calculate the probability of the whole matrix outcome.
I've tried the following code just as a start but it doesn't work yet and I can't wrap my head properly around how it works:
int counter = 0;
String [][] actualOutcome = {{"0","0","0"}, {"0","0","0"}, {"0","0","0"}, {"0","0","0"}, {"0","0","0"}, {"0","0","0"}, {"0","0","0"}, {"0","0","0"}};
String[] possibleOutcomes = {"0","1"};
for(int rowOne = 0; rowOne < 6; rowOne++) {
for(int rowTwo = 0; rowTwo < 3; rowTwo++) {
for (int possibilitiesCounter = 0; possibilitiesCounter < possibleOutcomes.length; possibilitiesCounter++) {
actualOutcome[rowOne][rowTwo] = possibleOutcomes[possibilitiesCounter];
System.out.println("Possibility " + counter + ": \n" + actualOutcome[0][2] + actualOutcome[0][1] + actualOutcome[0][0] + "\n" +
actualOutcome[1][2] + actualOutcome[1][1] + actualOutcome[1][0] + "\n" +
actualOutcome[2][2] + actualOutcome[2][1] + actualOutcome[2][0]);
counter++;
}
}
}
Thank you in advance!
Upvotes: 0
Views: 156
Reputation: 5455
This is fairly succinct, at least for Java anyway. The largest effort is in formatting the generated string into a rectangular matrix. We use the trick of iterating over numbers that are a power of 2 larger than we need - this ensures that toBinaryString()
returns a String
that's m*n+1
long - we then strip off the high bit.
In terms of probability of a 1 occurring at a given position - over all patterns a 1 will occur 2^(m*n-1)
times in each position, so I don't know that this helps.
static void printMatrix(int m, int n)
{
for(int i = 1<<(m*n); i < 1<<(m*n+1); i++)
splitPrint(Integer.toBinaryString(i).substring(1), m, n);
}
static void splitPrint(String s, int m, int n)
{
for(int j=0; j<m; j++)
System.out.println(String.join(" ", s.substring(j*n, (j+1)*n).split("")));
System.out.println();
}
Called via printMatrix(6, 3);
Output:
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
<snip>
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Upvotes: 1
Reputation: 1735
Regarding first part of the requirement you can try this:
import java.util.Arrays;
class Scratch {
public static void main(String[] args) {
double counter = Math.pow(2, 9) - 1;
for (int i = 0; i <= counter; i++) {
int[][] matrix = {
{(i & (1 << 2)) >> 2, (i & (1 << 1)) >> 1, i & 1},
{(i & (1 << 5)) >> 5, (i & (1 << 4)) >> 4, (i & (1 << 3)) >> 3},
{(i & (1 << 8)) >> 8, (i & (1 << 7)) >> 7, (i & (1 << 6)) >> 6},
};
printArray(matrix);
}
}
private static void printArray(int[][] matrix) {
System.out.println("==================");
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
}
This will give output like:
==================
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
==================
[0, 0, 1]
[0, 0, 0]
[0, 0, 0]
==================
[0, 1, 0]
[0, 0, 0]
[0, 0, 0]
==================
[0, 1, 1]
[0, 0, 0]
[0, 0, 0]
Upvotes: 0
Reputation: 9650
Here's yet another solution:
int limit = (int)Math.pow(2, 3*3);
System.out.println(limit);
for (int counter = 0; counter < limit; ++counter) {
int x = counter;
for(int rowOne = 0; rowOne < 3; rowOne++) {
for(int rowTwo = 0; rowTwo < 3; rowTwo++) {
actualOutcome[rowOne][rowTwo] = possibleOutcomes[x%2];
x /= 2;
}
}
System.out.println("Possibility " + counter + ": \n" + actualOutcome[0][2] + actualOutcome[0][1] + actualOutcome[0][0] + "\n" +
actualOutcome[1][2] + actualOutcome[1][1] + actualOutcome[1][0] + "\n" +
actualOutcome[2][2] + actualOutcome[2][1] + actualOutcome[2][0]);
}
Upvotes: 1
Reputation: 341
The easiest way is to have a counter starting from 0
and going till 511
that is 111111111
in the binary system and instead of putting and manipulating with matrix just work with the number.
for (int i = 0; i < 512; i++) {
System.out.println("Possibility " + (i + 1) + ": ");
for (int j = 0; j < 9; j++) {
System.out.print((i & (1 << j)) >> j);
if (j % 3 == 2)
System.out.println();
else
System.out.print(" ");
}
}
Upvotes: 3
Reputation: 3454
your matrix can bes displayed as a line of elements:
matrix
[a] [b] [c]
[f] [e] [d]
[g] [h] [i]
line-representation
[a] [b] [c] [d] [e] [f] [g] [h] [i]
this line representation, when each value is between [0...1] would be analog to a binary number with 9 digits, going from 0 to 2^9 (which would be 512).
if you want to know all numbers from 0..512 then COUNT from 0 to 512 and convert that number back into the matrix
implementation note
for(int i = 0; i < 512; i++){
String asBinary = Integer.toBinaryString(i);
int elementA = Integer.parseInt(asBiniary.charAt(0));
int elementB = Integer.parseInt(asBiniary.charAt(1));
...
int elementG = Integer.parseInt(asBiniary.charAt(8));
}
Upvotes: 1