mixing ingredients from tables besides looping

4 views (last 30 days)
addy fang
addy fang on 28 Jul 2020
Answered: ag on 13 Nov 2024 at 18:39
I want to add certain percentages of data from several tables to one new table. The percentage of the ingredients added up should equal to 100%. The percentage of each ingredient can be from 0% to 100%. I need to go through and save all the possible compositions with distinct names, and do calculations in order to find out the best composition. For example, 10% database A + 90% database B + 0% database C, 10% database A + 80% database B + 10% database C, 10% database A + 70% database B + 20% database C, ...
Starting with two databases A and B, each with 4 columns of numerical data. I will need to produce and save all the mixed combination. For example, 10% of each column in A add 90% of the corresponding column in B, and save the result. The total percentage is 100%. This could be done with loop. for x=0:0.1:1, C=x*A+(1-x)*b, end. This seems straightforward, if I only have two databases A and B.
If I have three databases, A, B and C, to produce D. So I need two loops?
If I have A, B, C and D, to produce E, so I need to have three loops?
If I want to mix more, do I have to create another loop again?
As the number of the databases I want to mix increase, this looping solution seems inefficient. Because everytime the numer of database changes, I need to rewrite my code again.
I was wondering if there is any better solution for this problem. So it does not need me to redo the coding even if I change the number of databases to be mixed. I hope I only need one code to run whatever number of databases that I want to mix.
Thank you very much.

Answers (1)

ag
ag on 13 Nov 2024 at 18:39
Hi Addy,
I recommend using a more generalized approach that doesn't require manually adding loops each time you increase the number of databases.
One possible approach would be to generate all possible combinations of percentages that sum to 1 (100%) and then apply each combination to compute the weighted sum of the databases.
The below code snippet illustrates the above approach:
function compositions = mixDatabases(databases, step)
% databases: cell array of matrices (databases)
% step: percentage step size (e.g., 0.1 for 10%)
numDatabases = numel(databases);
percentages = 0:step:1;
compositions = {};
% Generate all combinations of percentages that sum to 1
combinationMatrix = generateCombinations(numDatabases, percentages, 1);
% Iterate over each combination
for i = 1:size(combinationMatrix, 1)
weights = combinationMatrix(i, :);
% Initialize a zero matrix for the resulting composition
result = zeros(size(databases{1}));
% Calculate the weighted sum for the current combination
for j = 1:numDatabases
result = result + weights(j) * databases{j};
end
% Save the result with a distinct name
compositions{end+1} = struct('name', sprintf('Composition_%d', i), 'data', result, 'weights', weights);
end
end
function combinations = generateCombinations(numElements, values, targetSum)
% Recursive function to generate combinations of values that sum to targetSum
if numElements == 1
% Base case: only one element left
combinations = values(values == targetSum)';
else
combinations = [];
for v = values
if v <= targetSum
subCombinations = generateCombinations(numElements - 1, values, targetSum - v);
if ~isempty(subCombinations)
combinations = [combinations; [v * ones(size(subCombinations, 1), 1), subCombinations]];
end
end
end
end
end
Hope this helps!

Community Treasure Hunt

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

Start Hunting!