Generating a matrix with all possible combinations of values

R

RSH

Okay my math skills aren't waht they used to be...

With that being said what Im trying to do is create a matrix that given x
number of columns, and y number of possible values i want to generate a two
dimensional array of all possible combinations of values.

A simple example:
2 - columns and 2 possible values would generate:

0 0
1 0
0 1
1 1

I obviously need to generate considerably more complex grids but Imagine the
math would be the same.

Thanks for any assistance!
Ron
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

And how you want to keep it?

By using a recursive method this is simple. you can do a similar ting with a
nested number of loops ( in case you know the dimension beforehand)
 
N

Nicholas Paldino [.NET/C# MVP]

Ron,

Well, the total number of values is going to be the number of values,
taken to the power of the number of columns that can contain those values.
In this case, it is 2 (number of possible values, true/false) squared (the
number of variables) which gives you four.

If you had three columns, you would have 8 possible combinations.

To represent this in code, you just need a two-dimensional array, like
so, where the first dimension is wich combination of values you want, and
the second column is the actual value in that combination. Using the two
values, three variable approach, you would want an 8x3 array, like so:

int rows = 8;
int columns = 3;

bool[,] matrix = new bool[rows, columns];

To initialize the array, the easiest way to populate it would be to use
a recursive algorithm, as Ignacio suggests.
 
C

Chris Shepherd

RSH said:
Okay my math skills aren't waht they used to be...

With that being said what Im trying to do is create a matrix that given x
number of columns, and y number of possible values i want to generate a two
dimensional array of all possible combinations of values.

A simple example:
2 - columns and 2 possible values would generate:

0 0
1 0
0 1
1 1

I obviously need to generate considerably more complex grids but Imagine the
math would be the same.

In addition to what other people have said, note that your matrices
generated will contain potentially redundant values (depending on your
intended use). In terms of unique number combinations, (0 and 1) and (1
and 0) are the same.

This of course only matters if you don't care about position in the matrix.

Chris.
 
R

RSH

I suppose I could use a datatable as an output. I used a simple binary flag
as the value array, but in reality I could have x number of values and x
number of rows which for some reason Im hanging up on. As far as the
position within the matrix, that isn't important, I just need to ensure that
every row is unique and that I have all possible permientations accounted
for.

A more realistic sample would be say I have 3 columns (Database1 Value,
Database 2 Value, Form Value)
Then 6 possible values (DBNull.Value,
"Weekly","Bi-Weekly","Semi-Monthly","Monthly","Quarterly")

I need to be able to pump this in to my algorithm and out put a datatable
where I ultimately will make a logi decision table out of it probably using
binary math.

Thanks for the replies!
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top