Array befüllen
12.01.2019 16:47:12
Steve
Ich versuche enen Code anzupassen, der Permutationen mit Wiederholung berechnet.
der code funktioniert fantastisch und extrem schnell, viel schneller als ein von mir programmierter Code.
Leider läuft bei 11 feldern ein Index über und ich verstehe leider nicht, welcher und warum. nach allem, was ich über Limitationen gehört habe sollte das noch gehen.
wenn ich die Zeilen-Variable lrow ausgebe , so ist bei ca 2 490 000 schluss.
Könnte Ihr mir sagen, welcher Index überläuft und warum?
Dim BufferPtr As Long
Dim lrow As Long
Option Explicit
' PGC - AUG 2016
' Permutation with repetition and with restrictions.
' A set of distinguishable objects is given, as well and the number of times _
each one is repeated
' Ex. Permutations with repetition with 2 "a"'s, 1 "b" and 2 "c"'s
' Input in a nx2 table, first columns the elements and second column how many _
times they repeat
Sub PermMultRep()
Dim i%, j%, n%
Dim VarMax, VarQuot As Long
Dim vIn As Variant, vPerms As Variant, vPerm As Variant
lrow = 1
vIn = Range("A2", Range("A2").End(xlDown)).Resize(, 2).Value ' table of elements and number of _
times they repeat
ReDim vPerm(1 To Application.Sum(Application.Index(vIn, 0, 2))) ' array for the current _
permutation
ReDim vPerms(1 To Application.MultiNomial(Application.Index(vIn, 0, 2)), 1 To UBound(vPerm)) ' _
array to store all permutations
PermMultRep1 vIn, vPerm, vPerms, 1, lrow ' calculate all the permutations into the vPerms array
Debug.Print "testpunkt"
Columns("D").Resize(, UBound(vPerm) + 1).Clear ' clears columns for the output
Debug.Print UBound(vPerms, 1)
Debug.Print UBound(vPerms, 2)
Range("D2").Resize(UBound(vPerms, 1), UBound(vPerms, 2)).Value = vPerms ' writes _ the output _
in E2, down and across
If UBound(vPerms, 1) 0 Then
vPerm(lInd) = vIn(j, 1)
If lInd = UBound(vPerm) Then
If lrow Mod 10000 = 0 Then Debug.Print lrow
lrow = lrow + 1
For lCol = 1 To UBound(vPerm)
vPerms(lrow, lCol) = vPerm(lCol)
Next lCol
Else
v1 = vIn
v1(j, 2) = v1(j, 2) - 1
PermMultRep1 v1, vPerm, vPerms, lInd + 1, lrow
End If
End If
Next j
End Sub
die von mir benötigte ausgabe bezieht sich auf die EingabeA 4
B 2
C 2
D 2
E 1
F 1
G 1
wofür es knapp 6,5Millionen Möglichkeiten geben sollte.