# NEED HELP With an algorithm...I think...: 5 columns with 6 variables

#### blevco

##### New member
So I have this idea in my head, and finding it hard to represent mathematically. What I want to make is a generator of all possible unique outcomes from an array. But I don't think array is the correct term...matrix maybe? Here's a rough visual representation:

1 2 3 4 5
a g m s y
b h n t z
c i o u .
d j p v .
e k q w
f l r x

So you have 5 columns with 6 variables in each and every variable is unique. I actually want it to be 8 columns with around 10-15 possible variables per column. My goal is to be able to pick a couple variables from each column and have the algorithm generate all possible unique combinations of those variables. For example:

From Column 1 I want b and c
Column 2 just i
Column 3 just p
Column 4 v,u
Column 5 z

So it would give me...
b-i-p-v-z
b-i-p-u-z
c-i-p-v-z
c-i-p-u-z
and I believe that is all the possible unique combinations.

So just to sum up, I want to build an algorithm that will take 8 columns with 10-15 variables per column and let me pick however many I want out of each column and then generate all possible outcomes picking 1 variable from each column.
If this makes any sense and you could lend some help I would greatly appreciate it. I obviously am struggling with just getting the idea out of my head right now. Again, any help...MUCH appreciated.
Seth

#### mmm4444bot

##### Super Moderator
Staff member
… I don't think array is the correct term ... matrix maybe?
A matrix is an array; in general, you may use those terms interchangeably.

Code:
1     2     3     4     5
a     g     m     s     y
b     h     n     t     z
c     i     o     u     A
d     j     p     v     B
e     k     q     w     C
f     l     r     x     D
So you have 5 columns with 6 variables in each and every variable is unique …

My goal is to be able to pick [some] variables from each column and have [an] algorithm generate all possible unique combinations of those variables.

For example:

From Column 1 I want b and c
Column 2 just i
Column 3 just p
Column 4 v,u
Column 5 z

So [the algorithm] would give me:

b-i-p-v-z
b-i-p-u-z
c-i-p-v-z
c-i-p-u-z

and I believe that is all the possible unique combinations.
Okay, that example shows some combinations of five variables chosen from a set of seven.

There are actually 21 unique combinations possible (each containing five variables), chosen from your set of seven {b,c,i,p,u,v,z}.

(There's a formula for determining the number 21, too.)

Are you writing a computer program, to list these combinations?

There are many different algorithms, to do that. Google keywords: algorithm for listing combinations :cool:

#### blevco

##### New member
mmm4444bot, first of all thank you for the response.
I should have been more clear in my original post. I only want to take 1 variable from each column. So in the end there would be a string of variables made from 1 variable for each column. Think of it as a baseball lineup. Each position is a column..
Pitcher 1st Base 2nd base
Joe Jeff Dennis
Jake Dave Roy..
Tim Matt .....and so on..
George Seth

To have a full lineup I would need 1 player from each position.
So I would go through each position(column) and pick 2 or 3 guys I want, tell the algorithm and it would generate all the possible lineups(combinations).

So in my example, those were the only unique combinations with those restrictions.
As for writing a program, I was hoping this would be simple enough to do in a spreadsheet as I am a beginner in coding at best!

A matrix is an array; in general, you may use those terms interchangeably.

Okay, that example shows some combinations of five variables chosen from a set of seven.

There are actually 21 unique combinations possible (each containing five variables), chosen from your set of seven {b,c,i,p,u,v,z}.

(There's a formula for determining the number 21, too.)

Are you writing a computer program, to list these combinations?

There are many different algorithms, to do that. Google keywords: algorithm for listing combinations :cool:

#### mmm4444bot

##### Super Moderator
Staff member
… I only want [1 variable] from each column [in any given combination].

… I was hoping this would be simple enough to do in a spreadsheet as I am a beginner in coding at best!
I got it, now.

Do you understand nested loops (i.e., loops running inside of other loops)?

If you put your picks from each column into separate lists, you could loop through each list, taking one element at a time, to form the combinations. The picks from the first column would be handled in the outermost loop; the picks from the last column would be handled in the innermost loop. :cool:

PS: I've never worked with spreadsheet software, but I'm sure the program flow would be the same.