File Exchange

image thumbnail

cofactors

version 1.1.0.0 (1.3 KB) by Gordon Amoako
This is a function that computes the cofactors of an n x n matrix

4 Downloads

Updated 10 Sep 2010

View Version History

View License

When a user enters an n x n matrix eg. A=[1 1 1; 3 1 -1; 2 1 -1] the function computes the corresponding cofactors.

Cite As

Gordon Amoako (2021). cofactors (https://www.mathworks.com/matlabcentral/fileexchange/28672-cofactors), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Lateef Kareem

Another issue with bruno's suggestion is that we still have to take the transpose of the result to get the cofactor matrix

Mark

Bruno's suggestion only works for invertible matrices

Bruno Luong

Cofactor_of_a = inv(a).'*det(a)

Simpler and faster.

Bruno

Sean de

The output is not preallocated and grows inside the loop: M-LINT!!!

This will do the same thing faster (with preallocating output):
[r,c]=size(A);
[rr cc] = meshgrid(1:r,1:c);
D = zeros(r,c);
for ii = 1:numel(A)
D(ii) = det(A(~(rr(ii)==1:r),~(cc(ii)==1:c)));
end
Ab = (-1).^(rr+cc).*D';

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!