Search code examples
algorithmmatlabsetdata-partitioning

Matlab partition problem


My head gets stucked finding an algorithm for my problem.

Assume I have N Numbers (lets say 4) and I want have ALL X-Partitions (X = N/2)

Example:

2-Partitions of {1,2,3,4} are: (1,2) (1,3) (1,4) (2,3) (2,4) (3,4) [Simply: all combinations]

I don't have a clue how to generate these combinations. If someone of you have an idea in Mind (I don't care what language. Pseudocode ist totally enough. I don't care if it's iterative or explicit).

Best regards, Bigbohne


Solution

  • Matlab has a function for this:

    http://www.mathworks.com/help/techdoc/ref/nchoosek.html

    >> x = [1,2,3,4]
    
    x =
    
    1     2     3     4
    
    >> nchoosek(x, 2)
    
    ans =
    
     1     2
     1     3
     1     4
     2     3
     2     4
     3     4
    

    Loop constructs like maxwellb's are awfully slow in matlab...