How can I perform combinatorics in MATLAB?
12 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Abhimenyu
2024-4-15
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
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!