Generate matrix combinations with parameters

4 views (last 30 days)
I have the following matrix (8x6):
M = [T_1 T_2 T_3 0 0 0
T_1 0 T_2 T_3 0 0
T_1 0 0 T_2 T_3 0
T_1 T_2 0 0 T_3 0
0 T_1 T_2 0 0 T_3
0 0 T_1 T_2 0 T_3
0 0 0 T_1 T_2 T_3
0 T_1 0 0 T_2 T_3]
where T has the following possibilities: {1,0,0}, {0,1,0}, {0,0,1} or {1,1,1} and T_i is the i-component of T.
How can I create all possible combinations for M?

Accepted Answer

Shantanu Dixit
Shantanu Dixit on 11 Jul 2024
Edited: Shantanu Dixit on 11 Jul 2024
Hi Catarina,
It is my understanding that you are trying to generate the all possible combinations for the matrix M using T row vectors.
I am assuming that for each row T can take one of following possible 4 values
1. {1,0,0}
2. {0,1,0}
3. {0,0,1}
4. {1,1,1}
So for each row, there are 4 options available to fill that row.
No. possible combinations = 4*4*4*.. (8 times) = 4^8 = 65536
To generate all possible combinations recursion can come handy, you can see the below code for reference.
% All possibilities for T
% The initial matrix with symbolic placeholders (1, 2, 3)
% representing t1, t2, t3
T_possibilities = [
1, 0, 0;
0, 1, 0;
0, 0, 1;
1, 1, 1
];
% Initialize the original matrix M with symbolic placeholders
M_template = [
1, 2, 3, 0, 0, 0;
1, 0, 2, 3, 0, 0;
1, 0, 0, 2, 3, 0;
1, 2, 0, 0, 3, 0;
0, 1, 2, 0, 0, 3;
0, 0, 1, 2, 0, 3;
0, 0, 0, 1, 2, 3;
0, 1, 0, 0, 2, 3
];
% All possibilities for T
% The initial matrix with symbolic placeholders (1, 2, 3)
% representing t1, t2, t3
T_possibilities = [
1, 0, 0;
0, 1, 0;
0, 0, 1;
1, 1, 1
];
% Initialize the original matrix M with symbolic placeholders
M_template = [
1, 2, 3, 0, 0, 0;
1, 0, 2, 3, 0, 0;
1, 0, 0, 2, 3, 0;
1, 2, 0, 0, 3, 0;
0, 1, 2, 0, 0, 3;
0, 0, 1, 2, 0, 3;
0, 0, 0, 1, 2, 3;
0, 1, 0, 0, 2, 3
];
% Function to generate all combinations recursively
function combinations = generate_combinations(M_template, T_possibilities, row, combinations)
if row > size(M_template, 1)
combinations{end+1} = M_template;
return;
end
for i = 1:size(T_possibilities, 1)
T = T_possibilities(i, :);
M_row = M_template(row, :);
for j = 1:3
% replace the placeholders (1, 2, 3) with the corresponding
% elements from T
M_row(M_row == j) = T(j);
end
new_template = M_template;
new_template(row, :) = M_row;
combinations = generate_combinations(new_template, T_possibilities, row + 1, combinations);
end
end
% Generate all possible combinations
all_combinations = generate_combinations(M_template, T_possibilities, 1, {});
% Display the number of combinations
fprintf('Total combinations: %d\n', length(all_combinations));
Total combinations: 65536
disp('Example combinations:');
Example combinations:
%% Display sample combination
disp(all_combinations{1});
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
disp(all_combinations{2});
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0
The above MATLAB code defines a matrix M with symbolic placeholders (1, 2, 3) and a set of possible transformation matrices T. It recursively generates all combinations of M by replacing placeholders with elements from T. Each combination results in a modified matrix M, and all such combinations are stored in 'all_combinations'.
Thanks
  1 Comment
Catarina Pina
Catarina Pina on 11 Jul 2024
The length of all_combinations is 65536. If I have a condition to impose to these combinations and eliminate some of them (I have already made this with if ... all_combinations{i} = []), how can I update the size of all_combinations? The entries remain in the structure, although empty, but I want to eliminate them completely (shifting all the others). For instance, if I delete 2 entries, how can I update the size of all_combinations to 65534?

Sign in to comment.

More Answers (4)

Omega
Omega on 10 Jul 2024
Edited: Omega on 10 Jul 2024
Hi Catarina,
To generate all possible combinations for the matrix M with the given parameters, you can use MATLAB to iterate through all possible values of T. Here’s a step-by-step approach to achieve this:
  1. We iterate through each possible T value from T_values.
  2. For each T value, we replace all placeholders for T_1, T_2, and T_3 in M with the corresponding components of the current T value.
  3. We directly store each generated matrix in the all_combinations cell array.
Below is a MATLAB script to accomplish this:
% Define the matrix M with placeholders
M = [1 2 3 0 0 0;
1 0 2 3 0 0;
1 0 0 2 3 0;
1 2 0 0 3 0;
0 1 2 0 0 3;
0 0 1 2 0 3;
0 0 0 1 2 3;
0 1 0 0 2 3];
% Define the possible values of T
T_values = {[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1]};
% Initialize a cell array to store all possible combinations of M
all_combinations = {};
% Iterate through all possible values of T
for T_idx = 1:length(T_values)
% Extract the current T value
T = T_values{T_idx};
% Create a copy of M to modify
M_comb = M;
% Replace placeholders with the corresponding T values
for i = 1:8
for j = 1:6
if M(i, j) == 1
M_comb(i, j) = T(1);
elseif M(i, j) == 2
M_comb(i, j) = T(2);
elseif M(i, j) == 3
M_comb(i, j) = T(3);
end
end
end
% Add the matrix to the combinations list
all_combinations{end+1} = M_comb;
end
% Display the number of unique combinations
disp(['Total number of unique combinations: ', num2str(length(all_combinations))]);
Total number of unique combinations: 4
% Display all unique combinations
for k = 1:length(all_combinations)
disp(['Combination ', num2str(k), ':']);
disp(all_combinations{k});
end
Combination 1:
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
Combination 2:
0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0
Combination 3:
0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
Combination 4:
1 1 1 0 0 0 1 0 1 1 0 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1
I hope this helps!
  1 Comment
Catarina Pina
Catarina Pina on 10 Jul 2024
Sorry, the problem is a little more complicated, I forgot to mention that T can vary in each line of M, that is, what we want is T_ij, with i =1,....,8, and j = 1,2,3,4, i.e., for example, in line 1 T could be 1 0 0, and in line 2 be 1,1,1, and so on.

Sign in to comment.


Catarina Pina
Catarina Pina on 11 Jul 2024
First of all, thank you for your solution! In fact, it solves the problem I described. But I ended up not explaining the problem completely well. Apologies for the entropy. In fact, I have the following matrix (8x6):
M = [T_11 T_12 T_13 0 0 0
T_21 0 T_22 T_23 0 0
T_31 0 0 T_32 T_33 0
T_41 T_42 0 0 T_43 0
0 T_51 T_52 0 0 T_53
0 0 T_61 T_62 0 T_63
0 0 0 T_71 T_72 T_73
0 T_81 0 0 T_82 T_83]
where T_ij, where i =1,....,8 corresponds to the row of the matrix, and T_i: has the following possibilities: {1,0,0}, {0,1,0}, {0,0,1} or {1,1,1} .
For instance, if T_{1:} = (1,0,0), T_{2:} = (1,1,1), T_{3:} = (0,1,0}), ..., we have the following combination:
[1 0 0 0 0 0
1 0 1 1 0 0
0 0 0 1 0 0
...
]
How can I create all possible combinations for M?

Catarina Pina
Catarina Pina on 11 Jul 2024
Thank you very much, it works perfectly!

Catarina Pina
Catarina Pina on 11 Jul 2024
The length of all_combinations is 65536. If I have a condition to impose to these combinations and eliminate some of them (I have already made this with if ... all_combinations{i} = []), how can I update the size of all_combinations? The entries remain in the structure, although empty, but I want to eliminate them completely (shifting all the others). For instance, if I delete 2 entries, how can I update the size of all_combinations to 65534?

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!