0

私は次のコードを持っています

B=[1 2 3; 10 20 30 ; 100 200 300 ; 500 600 800];
A=[100; 500; 300 ; 425];
SA = sum(A);
V={}; % number of rows for cell V = num of combinations -- column = 1                
n = 1;
 arr =  zeros(1,length(B))
for k = 1:length(B)                    
    for idx = nchoosek(1:numel(B), k)'  
       rows = mod(idx, length(B))
       if ~isequal(rows, unique(rows)) %if rows not equal to unique(rows)
           continue  %combination possibility valid      
       end %Ignore the combination if there are two elements from the same row 
       B_subset = B(idx)
       if (SA + sum(B_subset) <= 3000) %if sum of A + (combination) < 3000
           rows( rows==0 )=4
           arr(rows) = B_subset(:)
           V(n,1) = {arr}
           n = n + 1
            arr =  zeros(1,length(B))
        end
    end
end

AとBの一部の値の合計が3000未満の場合、組み合わせは有効であると見なされます。

私のコードの問題は、Bの最後の値がB(3,3)コードで一度しか考慮されていないことです。

コードを実行すると、行12にV含ま[0;0;0;800]れているセルが1つあることに気付くでしょう。ただし、。などの他の組み合わせも可能[1;0;0;800]です。ただしSA + (1 + 800) < 3000、コードはこの可能性を出力しません。

理由がわかりません。誰かがこれをデバッグして、いくつかの組み合わせがスキップされる理由を見つけてください。特にB(3,3)

4

1 に答える 1

0

この行は、意図したとおりに実行されていないと思われます。

if ~isequal(rows, unique(rows))

代わりに、これを試してください:

if ~isequal(length(rows), length(unique(rows)))
于 2013-03-08T06:56:12.310 に答える