Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 在循环中求和为一个值_Matlab - Fatal编程技术网

Matlab 在循环中求和为一个值

Matlab 在循环中求和为一个值,matlab,Matlab,希望有一个相对简单的方法来做到这一点,这只是我的愚蠢。 我试图找到一个盒子(大小)的组合,可以放入一个容量更大的盒子(盒子大小)。在下面的代码中,我现在把它作为geq,这显然是不好的,因为通常值都大于盒子的承载能力 boxSize = 9; size = [5 2 3 7 2 3 5 10 6 4]; % Box size Nos = [50 20 300 78 22 34 55 120 643 442]; % No things in box tmpMat = zeros(length(si

希望有一个相对简单的方法来做到这一点,这只是我的愚蠢。 我试图找到一个盒子(大小)的组合,可以放入一个容量更大的盒子(盒子大小)。在下面的代码中,我现在把它作为geq,这显然是不好的,因为通常值都大于盒子的承载能力

boxSize = 9;
size = [5 2 3 7 2 3 5 10 6 4]; % Box size
Nos = [50 20 300 78 22 34 55 120 643 442]; % No things in box

tmpMat = zeros(length(size),2); % Box size and number in there need to be linked
tmpMat(:,1)=size;
tmpMat(:,2)=Nos;

Order = tmpMat(randperm(length(tmpMat)),:); % Randomly sort

boxVector = [];

while(~isempty(Order)>0)                     % Determine which contributes to box
    sumMyBox=cumsum(Order);  
    sum2=find(sumMyBox(:,1)>=boxSize,1,'first');   % ISSUE here with geq 
    Used=Order(1:sum2,1);
    box = sumMyBox(sum2,:);
    boxVector = [boxVector; box];
    Order=Order((sum2+1):end,:);
end
简单地制作这个leq的问题是,它只取一个值,因为2小于10(例如),而我要寻找的是将数字相加,并尽可能接近boxSize。有什么建议吗?所有的编码都在Matlab中


提前感谢。

一个可能的解决方案是:

sum2=find(sumMyBox(:,1)>boxSize,1,'first') - 1;
找到溢出点并逐个备份


在您当前的测试用例中,如果大小为10的框位于列表的第一位,则答案为“无”。我可能不完全理解您的任务,但似乎您可能希望计算所有可能的框组合。或者,把它们从大到小排序,然后拿一个适合的最大的盒子,诸如此类。希望我不能完全理解您要解决的任务。

可能是匈牙利算法的任务<代码>