Find minors of a rectangular matrix
207 次查看(过去 30 天)
显示 更早的评论
I have a rectangular 3x2 matrix at hand and I need to find the minors in an automated way so that the code can be used for any rectangular/square matrix I may encounter in future. I currently can find the minors manually for the existing matrix at hand but I want to automate the process through some loop. Any suggestions on how I can do it?
2 个评论
回答(2 个)
John D'Errico
2022-7-12
编辑:John D'Errico
2022-7-12
Sadly, it is often true that people ask confusing questions, using their own terminology, jargon they have picked up that is often not standard. I'll assume that you know what a minor is for my answer, although it is entirely possible you are thinking something different.
A minor of a matrix is what remains of the matrix when you have removed one row and one column. So the minors of a rectangular matrix will also be rectangular. If the matrix was not square, then so will be the minor you have chosen.
However, you need to recognize there will be MANY such minors, depending on the size of your matrix. That is, for ANY element of the matrix, there will be a corresponding minor, since that element denotes a specific row and column. For an nxm matrix, there will be n*m minors, and each will be an (n-1)x(m-1) matrix.
One could certainly use a simple loop to create all of them. Perhaps storing them in a cell array. Or you could even store them all in the higher order planes of a 4-dimensional array. However, I fail to see a simple solution that would be better than a direct pair of nested loops to create them all. For example, your 3x2 array will have 6 minors, all of which are 2x1 arrays (actually, in this case, vectors).
A = randi(6,[3,2]) % choosing a random 3x2 matrix
allminors(A)
The code I wrote is not inefficient at all. It preallocates the array Aminors, so even for larger problems, there will be no problem. So if A is larger, it is still reasonably fast. Loops are not always a bad thing in MATLAB, as long as you understand how to use them properly.
A = randi(9,[50,100]);
B = allminors(A);
size(B)
timeit(@() allminors(A))
What you will do with them all is your problem. Though in the back of my mind, I still wonder if you do know what is a minor, or if you have good reasons for wanting to do this. Sorry, but when people ask basic questions like this using jargon that students often misuse, that is a distinct possibility. Perhaps you wanted to compute the determinants of the square minors. I cannot know.
function Aminors = allminors(A)
% generate all minors of a matrix
[n,m] = size(A);
Aminors = zeros(n-1,m-1,n,m);
deleteindex = @(ind,p) setdiff(1:p,ind);
for i = 1:n
for j = 1:m
Aminors(:,:,i,j) = A(deleteindex(i,n),deleteindex(j,m));
end
end
end
3 个评论
John D'Errico
2022-7-12
True. But if you look at other sources, you can find where the matrix formed by deleting exactly one row and one column is also called a minor, and this was then applied to any rectangular matrix.
I'm wondering who is correct here. Just a guess in the end.
Bjorn Gustavsson
2022-7-12
Yes, this guessing-game actually makes me more curious about who's closer. (We had a world-famous (on a national scale) 19th century poet that got famous for writing: "Det dunkelt sagda är det dunkelt tänkta". Our 19th century culture was rather mouldy, but that line stands the test of time.)
Bjorn Gustavsson
2022-7-12
编辑:Bjorn Gustavsson
2022-7-12
As far as I understand this one definition of generating one k-minor from an arbitrary matrix of size n-by-m is to select k rows and k columns, where k is smaller than both n and m. (There will be a rather large number of minors for larger matrices). One way to calculate that is:
function Minor = Mminor(M,I1,I2)
% Minor - calculate one matrix-minor
% Calling:
% Minor = Mminor(M,I1,I2)
% Input:
% M - double matrix, [n x m]
% I1 - row-index-array, positive integer array [1 x k], or [k x 1]
% I2 - column-index-array, positive integer array [1 x k], or [k x 1]
% Output
% Minor - det(M(I1,I2)), one matrix minor.
% Example:
% I1 = [1 2 4 6];
% I2 = [2 3 4 5];
% M = randn(7,5);
% M1 = Mminor(M,I1,I2)
% det(M(I1,I2))
% The largest element in I1 has to be smaller than or equal to n, the
% largest element in I2 has to be smaller than or equal to m
%
Minor = det(M(I1,I2));
end
You will have to introduce error-checks and other stuff.
HTH
3 个评论
Bjorn Gustavsson
2022-7-12
@John D'Errico: Well, at least one of us are not exactly correct - and that might be the best we can hope for in this situation.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!