Here's an example from my Linear Algebra book:
A = [1 2 3
2 3 4
3 4 5]
From the answers in the back of the book, the adjunct matrix is,
adj A = [-1 2 -1
2 -4 2
-1 2 -1]
I've been using det(A) in Matlab to compute the determinant and check
my work. Is there a similar function to compute the adjunct matrix?
In my experience, I have never actually needed the adjoint explicitly
for anything. It is computed from the determinants of all the minors
and is used in defining the matrix inverse (an object I have needed
explicitly only very rarely).
What are you trying to do that requires the adjoint?
-Greg
I'm trying to check my Linear Algebra homework. ^_-
Since I'm feeling generous, here's an adjoint function:
function B=adj(A)
n=size(A,1); d=1:n-1;
B=zeros(n);
AA=[A,A;A,A]';
for j=1:n
for k=1:n
B(j,k)=(-1)^(j+k)*det(AA(j+d,k+d));
end
end
Don't use this for big matrices!
HTH,
Greg
> I see. I'd guess the reason there is not a Matlab function is that it
> would really only be useful for checking linear algebra homework.
>
> Since I'm feeling generous, here's an adjoint function:
>
> function B=adj(A)
>
> n=size(A,1); d=1:n-1;
> B=zeros(n);
>
> AA=[A,A;A,A]';
>
> for j=1:n
> for k=1:n
> B(j,k)=(-1)^(j+k)*det(AA(j+d,k+d));
> end
> end
>
> Don't use this for big matrices!
>
> HTH,
>
> Greg
-----------------------
Greg, if you leave out the factor (-1)^(j+k), you'll have it right.
Roger Stafford
function B=adj(A)
n=size(A,1); d=1:n-1;
B=zeros(n); AA=[A,A;A,A]';
for j=1:n
for k=1:n
B(j,k)=det(AA(j+d,k+d));
end
end
Still, let me re-emphasize that this is only useful as an educational
aid.
The word adjoint is ambiguous. The above is also known as the
adjugate of matrix A. Formally, the adjoint is the same as the
conjugate transpose of A.
B = A.' ; % adjoint
B = det(A) * inv(A) ; % adjugate, for square invertible matrices
hth
Jos
> B = A.' ; % adjoint
should be B = A' of course ...
Jos
I should have thought of this myself. In the very chapter I'm
studying I learned that,
inv(A) = 1/det(A) * adj(A)
(in fact, this is why I'm computing adj(A) in the first place, but
I'm doing it by hand and want to be able to check it with Matlab)
I guess it makes sense that,
adj(A) = det(A) * inv(A)
Thanks.
-Greg
> That is the case when A is nonsingular. However, adj(A) will always
> exist even if inv(A) does not.
>
> -Greg
---------------------
I believe the 'svd' function can be used to find the adjoint (or
adjugate) of a square matrix A even if it is not invertible. This method
avoids having to tediously compute n^2 different n-1 x n-1 cofactor
determinants.
n = size(A,1);
[u,s,v] = svd(A);
s = diag(s);
ix = toeplitz(ones(n-1,1),[1 zeros(1,n-1)])+repmat((1:n-1)',1,n);
B = v*diag(prod(reshape(s(ix),n-1,n)))*u'/det(v*u'); % B = adj(A)
(To ease the understanding of index ix, I point out that each of the terms
used in the diagonal matrix for obtaining B in the last line is the
product of all but one of the diagonal terms in s.)
Roger Stafford
> I believe the 'svd' function can be used to find the adjoint (or
> adjugate) of a square matrix A even if it is not invertible. This method
> avoids having to tediously compute n^2 different n-1 x n-1 cofactor
> determinants.
>
> n = size(A,1);
> [u,s,v] = svd(A);
> s = diag(s);
> ix = toeplitz(ones(n-1,1),[1 zeros(1,n-1)])+repmat((1:n-1)',1,n);
> B = v*diag(prod(reshape(s(ix),n-1,n)))*u'/det(v*u'); % B = adj(A)
>
> (To ease the understanding of index ix, I point out that each of the terms
> used in the diagonal matrix for obtaining B in the last line is the
> product of all but one of the diagonal terms in s.)
>
> Roger Stafford
------------------
I think it is conceptually better to write that last line as:
B = det(u*v')*v*diag(prod(reshape(s(ix),n-1,n)))*u'; % B = adj(A)
though the answer is the same. This way shows its derivation better from
det(A)*inv(A) = det(u*s*v') * v*inv(s)*u'
= det(u*v') * v*(inv(s)*det(s))*u'
as a limiting case as A approaches singularity. After cancellation, the
diagonal terms in inv(s)*det(s) each have all but one of the terms in s,
and a zero-divided-by-zero situation is avoided.
Roger Stafford
------------------
Hello Greg,
You were too quick to accept the correction concerning the factor
(-1)^(j+k) which I made yesterday to your "educational" adj function. It
turns out you need it in case n = size(A,1) is even but not if n is odd.
The OP's case was odd and the factor would then be incorrect, so I jumped
to the erroneous conclusion that it should never be used. Doh! for me
too.
Roger Stafford
function B=adj(A)
n=size(A,1); d=1:n-1;
B=zeros(n); c=2*mod(size(A,1),2)-1;
AA=[A,A;A,A]';
for j=1:n
for k=1:n
B(j,k)=c^(j+k)*det(AA(j+d,k+d));
end
end