필터 지우기
필터 지우기

generate all possible upper triangular matricies with variables

조회 수: 2 (최근 30일)
Alec Bledsoe
Alec Bledsoe 2023년 10월 25일
댓글: Dyuman Joshi 2023년 10월 25일
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
Torsten
Torsten 2023년 10월 25일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 10월 25일
편집: Voss 2023년 10월 25일
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)
(:,:,1) = 1 0 0 0 1 0 0 0 1 (:,:,2) = 1 0 0 0 1 1 0 0 1 (:,:,3) = 1 0 0 0 1 2 0 0 1 (:,:,4) = 1 0 1 0 1 0 0 0 1 (:,:,5) = 1 0 1 0 1 1 0 0 1 (:,:,6) = 1 0 1 0 1 2 0 0 1 (:,:,7) = 1 0 2 0 1 0 0 0 1 (:,:,8) = 1 0 2 0 1 1 0 0 1 (:,:,9) = 1 0 2 0 1 2 0 0 1 (:,:,10) = 1 1 0 0 1 0 0 0 1 (:,:,11) = 1 1 0 0 1 1 0 0 1 (:,:,12) = 1 1 0 0 1 2 0 0 1 (:,:,13) = 1 1 1 0 1 0 0 0 1 (:,:,14) = 1 1 1 0 1 1 0 0 1 (:,:,15) = 1 1 1 0 1 2 0 0 1 (:,:,16) = 1 1 2 0 1 0 0 0 1 (:,:,17) = 1 1 2 0 1 1 0 0 1 (:,:,18) = 1 1 2 0 1 2 0 0 1 (:,:,19) = 1 2 0 0 1 0 0 0 1 (:,:,20) = 1 2 0 0 1 1 0 0 1 (:,:,21) = 1 2 0 0 1 2 0 0 1 (:,:,22) = 1 2 1 0 1 0 0 0 1 (:,:,23) = 1 2 1 0 1 1 0 0 1 (:,:,24) = 1 2 1 0 1 2 0 0 1 (:,:,25) = 1 2 2 0 1 0 0 0 1 (:,:,26) = 1 2 2 0 1 1 0 0 1 (:,:,27) = 1 2 2 0 1 2 0 0 1

추가 답변 (2개)

Mann Baidi
Mann Baidi 2023년 10월 25일
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
Dyuman Joshi
Dyuman Joshi 2023년 10월 25일
Why should OP download a function file, when there are options/methods available utilizing built-in functions?

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2023년 10월 25일
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')
M = 27×1 cell array
{3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double} {3×3 double}
Let's look at one of the values to check it satisfies your requirements.
M{17}
ans = 3×3
1 1 2 0 1 1 0 0 1
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 10월 25일
@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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by