all permutations

K

KAH

Hi. I am trying to generate all possible single arrays which consist of one
element in each row of a double array in VisualBasic in Excel. For example,
if DoubleArray(R,C) is first row 1,2,3 and second row 4,5,6; then I want
NewArray(R) of (1,4), (1,5), (1,6), (2,4), (2,5) and (2,6). I have figured
out how to do this with specific numbers of rows and columns, but I want to
do this with a variable number of rows and columns (input by the user). Here
is what I have:
If Rows = 3 Then
For i = 1 To Columns
For j = 1 To Columns
For k = 1 To Columns
NewArray(1) = DoubleArray(1, i)
NewArray (2) = DoubleArray(2, j)
NewArray (3) = DoubleArray(3, k)
Functions
Next k
Next j
Next i
ElseIf Rows = 4 Then
For i = 1 To Columns
…
End If
'Functions' are simply what I want to do with the single arrays.
How can I change this so it can work with any number of rows and columns?
Thanks for any help.
 
J

Joel

Dim NewArray As Variant
NumberofRows = UBound(DoubleArray, 1)
NumberofCols = UBound(DoubleArray, 2)

NewArrayIndex = 1
ReDim NewArray(NumberofRows * NumberofCols)
For RowCount = 1 To NumberofRows
For ColCount = 1 To NumberofCols
NewArray(NewArrayIndex) = _
DoubleArray(RowCount, ColCount)
NewArrayIndex = NewArrayIndex + 1
Next ColCount
Next RowCount
 

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