繁体   English   中英

需要帮助在Matlab中调试此代码

[英]Need help debugging this code in Matlab

我有以下代码

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(3,3)的最后一个值B(3,3)在代码中只占一次。

如果运行代码,则会在第12行注意到V一个单元格包含[0;0;0;800] ,但是还有其他可能的组合,例如[1;0;0;800] SA + (1 + 800) < 3000 ,代码不会显示这种可能性。

我不知道为什么,有人可以帮我调试一下,找出为什么跳过某些组合吗? 特别是B(3,3)

我怀疑这条线没有完全按照您的意图执行:

if ~isequal(rows, unique(rows))

相反,请尝试以下操作:

if ~isequal(length(rows), length(unique(rows)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM