X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 19 Nov 2013 05:55 AM by  anon
How to get all the combinations of N arrays by choosing one element from each array at each time
 5 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:13
New Member


--
19 Nov 2013 05:55 AM
    Hi, everyone I have N arrays, take N = 3 as an example, array1 = [1, 2, 3], array2 = [4, 5] and array3 = [6, 7 ,8, 9]. I want to get one element from each array at each time to combine them into a new array that has N elements. For example, I choose 1 from array1, choose 4 from array2, and choose 6 from array3, then get a 3 elements newarray = [1, 4, 6]. I want to get all of these combinations( in this example, it has 3 * 2 * 4 = 24 combinations). My question is how to implement this by programming. Please note that N is a variable. so I do not know how to use the for loop when programming. Any suggestions will be greatly appreciated!

    Deleted User



    New Member


    Posts:
    New Member


    --
    02 Dec 2013 01:59 PM
    Hi Gou, My understanding is that the Numerical Recipies book has an algorithm for this combinatorial caculation. Maybe you could implement it in IDL? Also, I found this page that can be helpful: https://groups.google.com/forum/#!sea... I hope Numerical Recipies can help. Cheers, fernando

    Deleted User



    New Member


    Posts:
    New Member


    --
    02 Dec 2013 02:12 PM
    Hi Gou again, Yes, I think that Kenneth Bowman seems to have the answer to your question in that IDL Google forum page: https://groups.google.com/forum/#!sea... Cheers, fernando

    Deleted User



    New Member


    Posts:13
    New Member


    --
    08 Dec 2013 08:37 AM
    hi, fernando Thanks for your reply. It's true that Kenneth Bowman's method can partially solve my problem(I have used a similar method in my programming before I put forward my problem), however, as I have stated in my question that as the N(the number of arrays) is a variable,and I can only choose one element from these N arrays to get a combination of N elements at each time. What I mean is that if N = 2, I will use two for loops, if N =3 , 3 for loops will be used, if N = 6, 6 for loops will be used, and so on. It's difficult to determine how many for loops will be used in the programming.I want to adapt the for loops in programming intellectually according to the value of N. Can you give me some instructions? Thanks very much! PS: this is an example code used in my programming for N =3 ; make lookup table count = 0 FOR i = 0.0,n_elements(array1)-1 DO BEGIN FOR j = 0.0, n_elements(array2)-1 DO BEGIN FOR k = 0.0, n_elements(array3)-1 DO BEGIN lut[count,*] = [array1[i],array2[j],array3[k]] count = count+1 ENDFOR ENDFOR ENDFOR

    Deleted User



    New Member


    Posts:
    New Member


    --
    08 Dec 2013 04:38 PM
    Hi Gou, What about using a CASE statement?: http://www.exelisvis.com/docs/CASE.html How big can N become? fernando

    Deleted User



    New Member


    Posts:13
    New Member


    --
    08 Dec 2013 06:27 PM
    hi, fernando Thanks for your timely reply. the use of CASE statement is not a bad idea, though it will make the programming looks like verbose. In my case, the value of N may be as large as 10.
    You are not authorized to post a reply.