435,501 Members | 2,797 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Generating a matrix with all possible combinations of values

 P: n/a 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 Oct 11 '07 #1
4 Replies

 P: n/a 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) "RSH"

 P: n/a 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. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "RSH"

 P: n/a RSH wrote: 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. Oct 11 '07 #4

 P: n/a 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 "Chris Shepherd" 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 xnumber of columns, and y number of possible values i want to generate atwo dimensional array of all possible combinations of values.A simple example:2 - columns and 2 possible values would generate:0 01 00 11 1I obviously need to generate considerably more complex grids but Imaginethe 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. Oct 11 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion. 