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.
Gordon Amoako (2021). cofactors (https://www.mathworks.com/matlabcentral/fileexchange/28672-cofactors), MATLAB Central File Exchange. Retrieved .
Another issue with bruno's suggestion is that we still have to take the transpose of the result to get the cofactor matrix
Bruno's suggestion only works for invertible matrices
Cofactor_of_a = inv(a).'*det(a)
Simpler and faster.
The output is not preallocated and grows inside the loop: M-LINT!!!
This will do the same thing faster (with preallocating output):
[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)));
Ab = (-1).^(rr+cc).*D';
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!