Code covered by the BSD License  

Highlights from
cofactors

3.0

3.0 | 2 ratings Rate this file 5 Downloads (last 30 days) File Size: 1.3 KB File ID: #28672
image thumbnail

cofactors

by Gordon Amoako

 

09 Sep 2010 (Updated 10 Sep 2010)

This is a function that computes the cofactors of an n x n matrix

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
09 Sep 2010 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';

13 Sep 2010 Bruno Luong

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

Simpler and faster.

Bruno

20 May 2012 Mark

Bruno's suggestion only works for invertible matrices

Please login to add a comment or rating.
Updates
10 Sep 2010

I only did a preallocation of the array. I am going to take a look at Sean's suggestions.

Tag Activity for this File
Tag Applied By Date/Time
mathematics Gordon Amoako 09 Sep 2010 12:01:03
matrices Gordon Amoako 09 Sep 2010 12:01:03

Contact us at files@mathworks.com