Re: Square Matrix Determinant Recursive Algorithm - Help



On 2006-05-31 18:41 Alex Pupsa said the following:
Can someone help me?

I need to write a recursive function to calculate the determinant of
a square matrix. I'm having trouble because I don't really know how
recursive functions work in Matlab.

It works for 2x2 and 3x3 matrix.

Here's the code:

function f=determ(a,c)
[n,n]=size(a);
if n==2
f=a(1)*a(4)-a(2)*a(3);
else
for i=1:n
for j=1:n
if i==1
v1=i+1:n;
elseif i==n
v1=1:n-1;
else
v1=1:i-1;
v1(i)=i+1;
for k=i+1:n-1
v1(k)=v1(k-1)+1;
end
end
if j==1
v2=j+1:n;
elseif j==n
v2=1:n-1;
else
v2=1:j-1;
v2(j)=j+1;
for k=j+1:n-1
v2(k)=v2(k-1)+1;
end
end
f=c+((-1)^(i+j))*determ(a(v1,v2),c);
end
end
end

In Matlab I use it by typing: determ(matrix,0)

Hi!

Alex, matlab has a function that calculates the determinant. Use that instead. Are you checking for matrix singularity? If so use cond() instead.

HTH

PB

--
"Never attribute to malice that which can be adequately explained by stupidity."
.



Relevant Pages