How to eliminate combinations while looping?

I am given a 39x6 matrix (lets call it A) and I have to make a 6x6 matrix (B) . The last row of the 6x6 matrix is fixed and for the remaining 5 rows, all possible combinations of the initial A matrix's rows are taken 5 at a time to create B. Then the determinant of the B matrix is determined and the output are the cases when the determinant of B matrix is non zero.
The code I was able to do so far is given.
The issues are:
1) I am not able to come up with a system to eliminate the combinations that occur so that I can reduce the number of iterations.
2) I also couldn't figure out a way to store the matrices that have non zero determinant values.
also is there any possible way to make the code such that the it could have less loops?

2 Comments

1) I am not able to come up with a system to eliminate the combinations that occur so that I can reduce the number of iterations.
The code below uses nchoosek function to eliminate the combinations to be nCr = n!/(r!(n-r)!) = 575757 (n=39,r=5).
Also, perms function is used for making permutation nPr = n!/(n-r)! = 69090840 (n=39,r=5).
2) I also couldn't figure out a way to store the matrices that have non zero determinant values.
You can write it to console (or file) by disp function (or writematrix function or any other built-in functions).
also is there any possible way to make the code such that the it could have less loops?
The loops are reduced by using nchoosek function and perms function.
clear; close all;
Pos_E_Op = [0,0,0,0,-1,-1;-1,0,-1,0,0,-1;0,1,-1,0,0,-1;-1,1,0,0,0,-1;0,0,-1,1,0,-1;-1,0,0,1,0,-1;0,1,0,1,0,-1;0,-1,0,-1,-1,0;1,0,0,-1,-1,0;0,0,1,-1,-1,0;1,-1,0,0,-1,0;0,-1,1,0,-1,0;1,0,1,0,-1,0;0,0,-1,-1,0,0;-1,0,0,-1,0,0;0,1,0,-1,0,0;0,-1,-1,0,0,0;1,0,-1,0,0,0;-1,-1,0,0,0,0;0,0,0,0,0,0;1,1,0,0,0,0;-1,0,1,0,0,0;0,1,1,0,0,0;0,-1,0,1,0,0;1,0,0,1,0,0;0,0,1,1,0,0;-1,0,-1,0,1,0;0,1,-1,0,1,0;-1,1,0,0,1,0;0,0,-1,1,1,0;-1,0,0,1,1,0;0,1,0,1,1,0;0,-1,0,-1,0,1;1,0,0,-1,0,1;0,0,1,-1,0,1;1,-1,0,0,0,1;0,-1,1,0,0,1;1,0,1,0,0,1;0,0,0,0,1,1];
[M,N] = size(Pos_E_Op);
total_ite = factorial(M)/factorial(M-5); % nPr = n!/(n-r)! = 69090840 (n=39,r=5)
j = 1; i = 1;
text1 = "Running iteration : %d of %d";
text2 = "Possible combination %d";
clist = nchoosek(1:M, 5);
for m = 1:size(clist,1) % nCr = n!/(r!(n-r)!) = 575757 (n=39,r=5)
plist = perms(clist(m,:));
for n = 1:size(plist,1) % nPr / nCr = r! = 120 (r=5)
status = sprintf(text1,i,total_ite)
A = [Pos_E_Op(plist(n,:),:); [1, -1, 1, -1, 2, -2]];
i = i+1;
if det(A) ~= 0
sprintf(text2,j)
disp(A);
j = j+1;
end
end
end
2) I also couldn't figure out a way to store the matrices that have non zero determinant values.
Create a cell array that is the size of the maximum number of combinations. Initialize a counter to 0. Each time you find a suitable matrix, increment the counter and use the new counter as the index into the cell array to store the matrix.
At the end, cat(3, TheCell{:}) to get a 6 x 6 x something array of all of the matrices.

Sign in to comment.

Answers (0)

Categories

Asked:

on 19 May 2021

Commented:

on 17 Aug 2022

Community Treasure Hunt

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

Start Hunting!