Reputation: 135
I have the following code, written in Matlab, to find the combination MW of three units: Units 1, 2 and 3. The logic is the following:
Combine total of Unit 1, store it.
Combine total of Unit 2, store it.
Combine total of Unit 3, store it.
Combine total of Unit 1 and Unit 2, store it.
Combine total of Unit 1 and Unit 2 and Unit 3, store it.
Combine total of Unit 1 and Unit 3, store it.
Combine total of Unit 2 and Unit 3, store it.
Programcaticaly, I am doing it witht he following:
%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)
Is there a way to simplify the combination(i) block?
Upvotes: 1
Views: 74
Reputation: 221574
You can try this too -
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)))
A more general one could be presented for more number of inputs, if anyone is interested.
Upvotes: 2
Reputation: 8459
If the order is not important, then this code:
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))');
gives this:
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)
Upvotes: 2