Algorithm 简化以下代码的方法

Algorithm 简化以下代码的方法,algorithm,matlab,Algorithm,Matlab,我有以下用Matlab编写的代码,用于查找三台机组的组合MW:1号机组、2号机组和3号机组。逻辑如下: %Max MW for each unit maxMW = [200 60 50]; %Min MW for each unit minMW = [50 15 15] %Load Pattern in MW %1-2 3-4 5-6 7-8 loadPattern = [250 320 110 75] %F

我有以下用Matlab编写的代码,用于查找三台机组的组合MW:1号机组、2号机组和3号机组。逻辑如下:

%Max MW for each unit
maxMW = [200 60 50];

%Min MW for each unit
minMW = [50 15 15]


%Load Pattern in MW
              %1-2      3-4     5-6     7-8
loadPattern = [250      320     110     75]

%Full load production cost for each unit
productionCost = [15.4  16.0062 16.800  18.060  18.900]  

combination(1) = maxMW(1)
combination(2) = maxMW(2)
combination(3) = maxMW(3)
combination(4) = maxMW(1)+maxMW(2)
combination(5) = maxMW(1)+maxMW(2)+maxMW(3)
combination(6) = maxMW(2)+maxMW(3)
combination(7) = maxMW(1) + maxMW(3)
将1号机组的所有部件合并,并储存起来

组合2号机组的总重量,并将其储存起来

将3号机组的总功率合并,储存起来

将1号机组和2号机组的总数合并起来,储存起来

将1号机组、2号机组和3号机组的总数合并起来,储存起来

将1号机组和3号机组的总数合并起来,储存起来

将2号机组和3号机组的总数合并起来,储存起来

编程我是这样做的:

%Max MW for each unit
maxMW = [200 60 50];

%Min MW for each unit
minMW = [50 15 15]


%Load Pattern in MW
              %1-2      3-4     5-6     7-8
loadPattern = [250      320     110     75]

%Full load production cost for each unit
productionCost = [15.4  16.0062 16.800  18.060  18.900]  

combination(1) = maxMW(1)
combination(2) = maxMW(2)
combination(3) = maxMW(3)
combination(4) = maxMW(1)+maxMW(2)
combination(5) = maxMW(1)+maxMW(2)+maxMW(3)
combination(6) = maxMW(2)+maxMW(3)
combination(7) = maxMW(1) + maxMW(3)

有没有办法简化组合i块?

如果顺序不重要,则此代码:

M=[1 2 3];
S=0;
for i=1:length(M)
S=S+nchoosek(3,i);
end
combinations=sum((arrayfun(@str2num,num2str(dec2bin(1:S))).*repmat(M,S,1))');
给出了:

combinations(1)=M(3)
combinations(2)=M(2)
combinations(3)=M(2)+M(3)
combinations(4)=M(1)
combinations(5)=M(1)+M(3)
combinations(6)=M(1)+M(2)
combinations(7)=M(1)+M(2)+M(3)

如果订单不重要,则此代码:

M=[1 2 3];
S=0;
for i=1:length(M)
S=S+nchoosek(3,i);
end
combinations=sum((arrayfun(@str2num,num2str(dec2bin(1:S))).*repmat(M,S,1))');
给出了:

combinations(1)=M(3)
combinations(2)=M(2)
combinations(3)=M(2)+M(3)
combinations(4)=M(1)
combinations(5)=M(1)+M(3)
combinations(6)=M(1)+M(2)
combinations(7)=M(1)+M(2)+M(3)
你也可以试试这个-

combination(1:3) = maxMW(nchoosek(1:3,3));
combination([4 7 6]) = sum(maxMW(nchoosek(1:3,2)),2)'; %%//'
combination(5) = sum(maxMW(nchoosek(1:3,1)))
如果有人感兴趣,可以为更多的输入提供更通用的输入。

您也可以尝试-

combination(1:3) = maxMW(nchoosek(1:3,3));
combination([4 7 6]) = sum(maxMW(nchoosek(1:3,2)),2)'; %%//'
combination(5) = sum(maxMW(nchoosek(1:3,1)))

如果有人感兴趣,可以为更多的输入提供更通用的输入。

这太棒了!比我想象的要复杂一点。我需要15分来代表,但如果可以的话,我会的。再次感谢!这有点复杂,实际上Divakar的解决方案可能更适合您的应用程序。这太棒了!比我想象的要复杂一点。我需要15分来代表,但如果可以的话,我会的。再次感谢!这有点复杂,实际上Divakar的解决方案可能更适合您的应用程序。