Problems with permutation

H

Henrik

Hi,
The following macro is meant to permutate all possible
combinations of 10 arrays of numbers. However, I get error
messages when I try to run the macro. I suspect it might
be because the arrays (1-10) do not all have the same
lengths (the lenght of the arrays vary from 2 to 4
variables). However, the errors could be caused by another
problem. Does anyone have an idea of how to fix this
problem?
Thank you very much,
Henrik

Sub Permutate()

With Worksheets("input")

arr1 = .Range("D2:F2").Value
arr2 = .Range("D3:F3").Value
arr3 = .Range("D4:F4").Value
arr4 = .Range("D5:G5").Value
arr5 = .Range("D6:E6").Value
arr6 = .Range("D7:G7").Value
arr7 = .Range("D8:E8").Value
arr8 = .Range("D9:F9").Value
arr9 = .Range("D10:E10").Value
arr10 = .Range("D11:E11").Value

End With


rw = 1

For i = LBound(arr1, 2) To UBound(arr1, 2)
For j = LBound(arr2, 2) To UBound(arr2, 2)
For k = LBound(arr3, 2) To UBound(arr3, 2)
For l = LBound(arr4, 2) To UBound(arr4, 2)
For m = LBound(arr5, 2) To UBound(arr5, 2)
For n = LBound(arr6, 2) To UBound(arr6, 2)
For o = LBound(arr7, 2) To UBound(arr7, 2)
For p = LBound(arr8, 2) To UBound(arr8, 2)
For q = LBound(arr9, 2) To UBound(arr9, 2)
For r = LBound(arr10, 2) To UBound(arr10, 2)

With Worksheets("permutation")


.Cells(rw, 1) = arr1(1, i)
.Cells(rw, 2) = arr2(1, j)
.Cells(rw, 3) = arr3(1, k)
.Cells(rw, 4) = arr4(1, l)
.Cells(rw, 5) = arr5(1, m)
.Cells(rw, 6) = arr6(1, n)
.Cells(rw, 7) = arr7(1, o)
.Cells(rw, 8) = arr8(1, p)
.Cells(rw, 9) = arr9(1, q)
.Cells(rw, 10) = arr10(1, r)

End With

rw = rw + 1

Next: Next: Next: Next: Next: Next: Next: Next: Next: Next

End Sub
 
T

Tom Ogilvy

Your code does what it is written to do. It generates the 20736 unique
combinations of your numbers. If you want permutations, each row can be
permuted to produce 3628800 (permut(10,10)) permutations per row for a total
of
75,246,796,800 (20,736 x 3,628,000)

if you want to throw your 28 numbers into a group and pull out the 10 number
permutations, it would be

47,621,141,568,000 =permut(28,10)

Anyway, for whatever you want to define a permutation as, you code is giving
the combinations with each position in the combination restricted to the
entries in your respective rows.
 

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