generate all possible upper triangular matricies with variables
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Alec Bledsoe
el 25 de Oct. de 2023
Comentada: Dyuman Joshi
el 25 de Oct. de 2023
I am trying to find a way of generating all possible matricies with different combinations of elements in a list.
The base matrix is an upper triangular matrix:
g = [1,a,b;
0,1,c;
0,0,1]
In this case, the values for a,b, and c are in the list [0,1,2]. I want to generate every possible g based on the combination of the options for a,b, and c ie:
g = [1,0,0;0,1,0;0,0,1] , g = [1,1,0;0,1,0;0,0,1] , g = [1,1,1;0,1,0;0,0,1] , ...
Is there a good way to do this in Matlab?
1 comentario
Torsten
el 25 de Oct. de 2023
Hint:
Use "ndgrid" to create all possible triple combinations of {0,1,2} and insert these combinations in the matrix for a, b and c.
Respuesta aceptada
Voss
el 25 de Oct. de 2023
Editada: Voss
el 25 de Oct. de 2023
g = [1,NaN,NaN; 0,1,NaN; 0,0,1];
v = [0,1,2];
n_values = numel(v);
slots = find(isnan(g));
n_slots = numel(slots);
n_combos = n_values^n_slots;
M = v(1+dec2base(0:n_combos-1,n_values)-'0');
idx = (slots+numel(g)*(0:n_combos-1)).';
g_all = repmat(g,[1,1,n_combos]);
g_all(idx) = M;
format compact
disp(g_all)
0 comentarios
Más respuestas (2)
Mann Baidi
el 25 de Oct. de 2023
Hi Alec,
I understand you would like to generate all the possible combinations matrices. I would like to suggest you the below function from the File Exchange which will help you to find all the possible permutations of "a","b" and "c". You can download the zipped folder, extract it and add the path to MATLAB.
After that you can get the permutions by the following code:
r1 = permn([0 1 2],3)
This will give you all the possible values for "a","b" and "c" respectively.
After that you can simply make a loop and put the values of "a","b" and "c" in the matrices.
Here is the link of the tool
Hoping that this helps!
1 comentario
Dyuman Joshi
el 25 de Oct. de 2023
Why should OP download a function file, when there are options/methods available utilizing built-in functions?
Steven Lord
el 25 de Oct. de 2023
Since you're using release R2023b, you can use the combinations function introduced in release R2023a.
values = [0, 1, 2];
g = @(a, b, c) [1,a,b; 0,1,c; 0,0,1];
V = combinations(values, values, values);
M = rowfun(g, V, 'OutputFormat', 'cell')
Let's look at one of the values to check it satisfies your requirements.
M{17}
1 comentario
Dyuman Joshi
el 25 de Oct. de 2023
@Steven Lord, a nice idea, but it feels (to me) like brute force, as values has to be supplied manually n times, where n is the number of elements in values.
What if there are more elements say 6 or 8 or a bigger array with 15 such placeholders?
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!