function [B] = adj(A,mode)
%ADJ Matrix adjoint.
%
% ADJ(A) is the adjoint matrix of matrix A.
% A may be complex and any size, RxC
%
% usage: B = adj(A)
%
% If R <= C then the Right Adjoint is returned
% If R >= C then the Left Adjoint is returned
%
% The inverse of A is: INV(A) = ADJ(A)/det(A).
% Matrices that are not invertable still have an adjoint.
%
% See also SVD, PINV, INV, RANK, SLASH, DETT
%Paul Godfrey, October, 2006
[r,c]=size(A);
[u,s,v]=svd(A);
k=det(u)*det(v');
if r==1 | c==1, s=s(1); end
s=diag(s);
if exist('mode','var')
B=k*prod(s)*pinv(A);
else
for n=1:length(s)
p=s;
p(n)=[];
V(:,n)=k*prod(p)*v(:,n);
end
B=V*u(:,1:length(s))';
end
return
syms m
%Demos of this program are:
clc
A=[-m 1 0;0 -m 1;-18 -27 -10-m]
aa=adj(A)
disp('ADJOINT')
a*aa