# How can I perform combinatorics in MATLAB?

11 views (last 30 days)
Joel Schelander on 23 Mar 2021
Answered: Abhimenyu on 15 Apr 2024
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
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

Abhimenyu on 15 Apr 2024
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
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
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
for j = combo
% For households with three or more inhabitants, consider two vehicles
if HHPerson(j) >= 3
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
end
% Store the total load increase for combinations of this size
end
% Display the results
end
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.
I hope this helps!

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!