开发者

Matlab partition problem

开发者 https://www.devze.com 2023-01-14 23:13 出处:网络
My head gets stucked finding an algorithm for my problem. Assume I have N Numbers (lets say 4) and I want have ALL X-Partition开发者_如何学Cs (X = N/2)

My head gets stucked finding an algorithm for my problem.

Assume I have N Numbers (lets say 4) and I want have ALL X-Partition开发者_如何学Cs (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


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...


Here's matlab code,

myNums = [2,3,6,5];
for i = 1:size(myNums,2)
    combinationsSet{i} = nchoosek(myNums,i);
end


foreach i in SET
    foreach j in SET
        if i < j, SAY "I have a partition ($i,$j)"
    NEXT j
NEXT i

this depends on an iterating feature for your set, and operates in N^2 time.

For Matlab, check out the functions provided for you, e.g. combnk

0

精彩评论

暂无评论...
验证码 换一张
取 消