Results 1 to 3 of 3

Thread: array permutation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    78

    array permutation

    hi,
    i have a tricky problem. i have for example three arrays of different dimestion

    A =[1,2,3,4]
    B=[5,6,7]
    C=[8,9]

    i would like to generate all the different combinations of values in these arrays and the values inside the array should not be repeated.

    e.g. the resultset should be

    1,5,8
    2,5,8
    3,5,8
    4,5,8
    1,6,8
    2,6,8
    3,6,8
    4,6,8
    1,7,8
    2,7,8
    3,7,8
    4,7,8
    1,5,9
    2,5,9
    3,5,9
    4,5,9
    1,6,9
    2,6,9
    3,6,9
    4,6,9
    1,7,9
    2,7,9
    3,7,9
    4,7,9

    any idea how to do this for any number N of arrays?
    Thanks!!

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: array permutation

    VB Code:
    1. Dim A(1 To 4) As String
    2. Dim B(5 To 7) As String
    3. Dim C(8 To 9) As String
    4. A(1) = "1"
    5. A(2) = "2"
    6. A(3) = "3"
    7. A(4) = "4"
    8. B(5) = "5"
    9. B(6) = "6"
    10. B(7) = "7"
    11. C(8) = "8"
    12. C(9) = "9"
    13. For x = 1 To 4
    14.     For y = 5 To 7
    15.         For z = 8 To 9
    16.             Debug.Print A(x) & "," & B(y) & "," & C(z)
    17.         Next
    18.     Next
    19. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Posts
    78

    Re: array permutation

    Thanks for the input i figured out how to do it for a small number of cases by using FOR loops. but how would you do it if you have N arrays without having to write N FOR loops?
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width