Jump to content

2D Array Permuation


Recommended Posts

Anyone know of a quick way to perform a 2d permuation?

I have three settings [A, B, C], each that can have three states[1,2,3]

I'd like an array created of all the posible settings...like:

[[A1,B1,C1],

[A1,B1,C2]...etc]

Sort doesn't matter...any idea/tips/hints?

_ArrayPermute is perfect, but only for one dimention.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

well, it's shooting the probelm with a bazooka, but i get this...now to simplify...also, it's only going trhough combos with a1 (skipping a2, and a3 else too many combos):

#include <Array.au3>

Local $aArray[7] = ['a1','b1','b2','b3','c1','c2','c3']

;_ArrayDisplay($aArray, "Array Permuted")
Local $aNewArray = _ArrayPermute($aArray, ",") ;Using Default Parameters
$iUpperLim =  ubound( $aNewArray) - 1
_ArrayDisplay ( $aNewArray, "begin" )
$j = 1
for $i=1 to 5040
 If  stringLeft ( $aNewArray[$j], 1 )   = 'a' And _
  StringMid ( $aNewArray[$j], 4, 1 )  = 'b' And _
  StringMid  ( $aNewArray[$j], 7, 1 )  = 'c' Then
  $aNewArray[$j] = stringLeft($aNewArray[$j],8)
  ;_ArrayDisplay ( $aNewArray, $i )
  $j = $j + 1
 Else
  _ArrayDelete ( $aNewArray, $j )
  $iUpperLim = $iUpperLim - 1
 EndIf
 If $j > $iUpperLim Then ExitLoop
Next
_ArrayDisplay($aNewArray, "Array Permuted1")
$iUpperLim =  ubound( $aNewArray) - 1
$iUpperLim2 = $iUpperLim
$j = 1
for $i = 1 to $iUpperLim
 If $j + 1 < $iUpperLim2 Then
  If $aNewArray[$j] = $aNewArray[$j+1] Then
   _ArrayDelete ( $aNewArray, $j+1 )
   $iUpperLim2 = $iUpperLim2 - 1
  Else
   $j = $j + 1
  EndIf
 Else
  ExitLoop
 EndIf
Next

_ArrayDisplay($aNewArray, "Array simplified")
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

ha, i'm special:

#include <Array.au3>

dim $array[1]

$iTotal = 1

For $i = 1 to 3

For $j = 1 to 3

For $k = 1 to 3

ReDim $array[$iTotal+1]

$array[$iTotal]="A" & $i & ",B" & $j & ",C" & $k

$iTotal = $iTotal + 1

Next

Next

Next

_ArrayDisplay ( $array, "begin" )

done!

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Someone might find this helpfull, to create all the possible testing scenarios...the script as shown will permute 7 criteria, with 3 options each...returns an array of all the possibilities ( 3^7 = 2187).

You can add, or remove however many settings, and options for each setting, by manipulating the initial array

#include <Array.au3>
; Create your array of values to permute
Dim $array[7][2]=[[0,2], [0,2], [0,2], [0,2], [0,2], [0,2], [0,2]]
;_ArrayDisplay ( $array, "begin" )
Dim $asRecursivePermute[1][UBound($array)]
Dim $asCurrentLevelPermute[UBound($array)]

Global $iCurrentArrayLevel
Local $iCurrentArraySubScript
Call ("RecursivePermute", $array, 0, 0)
Func RecursivePermute($asCallersArray, $iCurrentArrayLevel, $iCurrentArraySubScript)
ReDim $asRecursivePermute[$iCurrentArrayLevel+1][UBound($asCallersArray)+1]
$iMax = UBound ( $asCallersArray )-1
; Set Min / Max for current loop
For $i = $asCallersArray[$iCurrentArraySubScript][0] To $asCallersArray[$iCurrentArraySubScript][1]
;For $i = 0 to 2
  ; Set the current value for the specific element in array, then call function for the next
  $asCurrentLevelPermute[$iCurrentArraySubScript] = $i
  If $iMax = $iCurrentArraySubScript Then
   ReDim $asRecursivePermute[$iCurrentArrayLevel + 1][UBound($asCurrentLevelPermute)]
   ; loop through $asCurrentLevelPermute, and add to the proper level of $asRecursivePermute
   For $j = 0 to UBound($asCurrentLevelPermute)-1
    $asRecursivePermute[$iCurrentArrayLevel][$j] = $asCurrentLevelPermute[$j]
   Next
   $iCurrentArrayLevel = $iCurrentArrayLevel + 1
  Else
   $iCurrentArrayLevel = Call ( "RecursivePermute", $asCallersArray, $iCurrentArrayLevel, $iCurrentArraySubScript + 1 )
  EndIf
Next
Return $iCurrentArrayLevel
EndFunc


_ArrayDisplay ( $asRecursivePermute, "begin" )
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

#include <Array.au3>

Dim $Array2DA[2][2] = [[1,2],[3,4]]
$aNewArray = Array2DPermuteTo_Array1D($Array2DA,",")
if Not @error Then _ArrayDisplay($aNewArray,"")

Dim $Array2DB[3][3] = [[1,2,3],[4,5,6],[7,8,9]]
$NewArray2D = Array2DPermute_To_Array2D($Array2DB,",")
if Not @error Then _ArrayDisplay($NewArray2D,"")

Func Array2DPermuteTo_Array1D(ByRef $Array2D,$sDelim = "")
if Not IsArray($Array2D) Then Return SetError(1)
Local $rows = UBound($Array2D) , $cols = UBound($Array2D, 2)
Local $TempArray1D[$rows * $cols] , $q = 0 , $aNewArray
For $i = 0 To $rows - 1
For $j = 0 To $cols - 1
$TempArray1D[$q] = $Array2D[$i][$j]
$q += 1
Next
Next
$aNewArray = _ArrayPermute($TempArray1D,$sDelim)
if @error Then Return SetError(1)
Return $aNewArray
EndFunc

Func Array2DPermute_To_Array2D(ByRef $Array2D,$sDelim = "")
if Not IsArray($Array2D) Then Return SetError(1)
Local $rows = UBound($Array2D) , $cols = UBound($Array2D, 2)
Local $TempArray1D[$cols] , $NewArray2D[$rows][1] , $aNewArray
For $i = 0 To $rows - 1
For $j = 0 To $cols - 1
$TempArray1D[$j] = $Array2D[$i][$j]
Next
$aNewArray = _ArrayPermute($TempArray1D,$sDelim)
if @error Then Return SetError(1)
ReDim $NewArray2D[$rows][UBound($aNewArray) - 1]
For $p = 1 To UBound($aNewArray) - 1
$NewArray2D[$i][$p - 1] = $aNewArray[$p]
Next
Next
Return $NewArray2D
EndFunc

صرح السماء كان هنا

 

Link to comment
Share on other sites

The first function might as well just use _ArrayPermute...the 2 arrays you have, as seperate entities, are joined together into the 1 array (which can be handled by _arrayPermute)

The second one, again, is just combining the _ArrayPermute data into a transposed 2d array.

Mine, gets every combo ;)

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

The first function might as well just use _ArrayPermute...the 2 arrays you have, as seperate entities, are joined together into the 1 array (which can be handled by _arrayPermute)

The second one, again, is just combining the _ArrayPermute data into a transposed 2d array.

Mine, gets every combo ;)

Dim $Array2DA[2][2] = [[1,2],[3,4]]
$aNewArray = Array2DPermute($Array2DA)
if Not @error Then _ArrayDisplay($aNewArray,"")

Func Array2DPermute(ByRef $Array2D)
if Not IsArray($Array2D) Or UBound($Array2D, 0)  <> 2 Then Return SetError(1)
Local $rows = UBound($Array2D) , $cols = UBound($Array2D, 2)
Local $TempArray1D[$rows * $cols] , $q = 0 , $aNewArray
For $i = 0 To $rows - 1
For $j = 0 To $cols - 1
$TempArray1D[$q] = $Array2D[$i][$j]
$q += 1
Next
Next
$aNewArray = _ArrayPermute($TempArray1D,",")
if @error Then Return SetError(1)
$icols = StringSplit($aNewArray[1],",")
Dim $pNewArray[UBound($aNewArray) - 1][$icols[0]]
For $p = 1 To $aNewArray[0]
$SplitArray = StringSplit($aNewArray[$p],",")
For $n = 1 To $SplitArray[0]
$pNewArray[$p - 1][$n - 1] = $SplitArray[$n]
Next
Next
Return $pNewArray
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...