How can I perform combinatorics in MATLAB?

14 visualizzazioni (ultimi 30 giorni)
Joel Schelander
Joel Schelander il 23 Mar 2021
Risposto: Abhimenyu il 15 Apr 2024 alle 14:22
I have 16 Appartments and 4 BEVs. This loop gives how much the maximum elextricity load of the household(Appartment) increases.
The load profiles are 'Appartment' or 'HH' for the appartments and 'BEV' for the BEVs. I have one condition saying if the household has 3 or more inhabitants, it is possible to own 2 vehicles.
nHousehold=16;
nBEV=4;
HHPerson=[4 2 4 2 1 2 4 1 2 4 2 1 2 2 3 1];
for i = 1:nHousehold
I=[];
HH = Appartment(:, i);
for j = 1:nBEV
%If the household has three or more inhabitants then there can be
%two vehicles
if HHPerson(i)>=3
for k=1:nBEV
if k==j
Vehicle=BEV(:, j);
I(j,k) = max(HH+Vehicle)./max(HH);
continue
end
Vehicle=BEV(:, j)+BEV(:, k);
I(j,k) = max(HH+Vehicle)./max(HH);
end
elseif HHPerson(i)<3
Vehicle=BEV(:, j);
I(j) = max(HH+Vehicle)./max(HH);
end
end
%This cell contain one matrix for each household, that express
%how much the demand will increase with one or two vehicles, all possible combinations
INCREASELOAD{i}=I;
end
Now I want to use combinatorics to find out how much the total load of 2 households increases and then 3,4,5...16 households.
How can I do this? I attached the INCREASELOAD cell array

Risposte (1)

Abhimenyu
Abhimenyu il 15 Apr 2024 alle 14:22
Hi Joel,
From the information shared, I inferred that you wanted to use combinatorics to find the total household increase of two to sixteen households. MATLAB's "nchoosek" function is used to find the combinations which can then be applied to find the total household increase by iterating in a loop over each combination. Please refer to the below mentioned example MATLAB code that incorporates the combinatorics in the scenario shared in the query:
% Given data
nHousehold = 16; % Total number of households
load("INCREASELOAD.mat"); % Please load the above given file
HHPerson = [4 2 4 2 1 2 4 1 2 4 2 1 2 2 3 1]; % Number of inhabitants per household
% Initialize a cell array to store total load increase for each combination size
totalLoadIncreaseByCombSize = cell(nHousehold - 1, 1);
% Iterate over each combination size from 2 to nHousehold
for combSize = 2:nHousehold
% Initialize the total load increase for the current combination size
totalLoadIncrease = 0;
Here, the "nchoosek" function will generate the possible combinations of the households based on the "combsize" of the running loop.
% Generate all combinations of households of the current size
combinations = nchoosek(1:nHousehold, combSize);
% Iterate over each combination
for i = 1:size(combinations, 1)
combo = combinations(i, :);
% Calculate the total increase for this combination
comboIncrease = 1; % Start with no increase (factor of 1)
for j = combo
% For households with three or more inhabitants, consider two vehicles
if HHPerson(j) >= 3
maxIncrease = max(INCREASELOAD{j}(:)); % Assumes INCREASELOAD{j} is a matrix or vector
else
% Otherwise, consider only one vehicle
maxIncrease = max(INCREASELOAD{j}(:)) - 1; % Subtract 1 to get the increase over the base load
end
comboIncrease = comboIncrease * maxIncrease; % Multiply increase factors
end
% Add the calculated increase for this combination to the total
totalLoadIncrease = totalLoadIncrease + comboIncrease;
end
% Store the total load increase for combinations of this size
totalLoadIncreaseByCombSize{combSize - 1} = totalLoadIncrease;
end
% Display the results
for i = 1:length(totalLoadIncreaseByCombSize)
fprintf('Total load increase for %d households: %.2f\n', i + 1, totalLoadIncreaseByCombSize{i});
end
Total load increase for 2 households: 1356.11 Total load increase for 3 households: 20708.86 Total load increase for 4 households: 216566.43 Total load increase for 5 households: 1645736.87 Total load increase for 6 households: 9406682.47 Total load increase for 7 households: 41274998.55 Total load increase for 8 households: 140576031.37 Total load increase for 9 households: 373024510.07 Total load increase for 10 households: 768920254.17 Total load increase for 11 households: 1218682411.95 Total load increase for 12 households: 1456294075.60 Total load increase for 13 households: 1268657664.04 Total load increase for 14 households: 759955933.41 Total load increase for 15 households: 279693763.56 Total load increase for 16 households: 47645420.45
The above code is an example of the use of "nchoosek" MATLAB function. The specific logic of increasing the load can be changed. I have used multiplicative increase to demonstrate. For each combination, it multiplies the maximum increase factors (considering households can have one or two vehicles based on the number of inhabitants) and sums these increases to find the total load increase for that combination size.
For more information on the "nchoosek" function, please follow this MATLAB R2024A documentation link: https://www.mathworks.com/help/matlab/ref/nchoosek.html
I hope this helps!

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by