Determinant of a Matrix Without Built in Functions

12 views (last 30 days)
I'm tasked with finding the determinant of an arbitrarily sized matrix entered by the user without using the det function. The only hint I have have been given was to use for loops. I am completely at a loss.
I started with making sure that the matrix was square:
a1 = input ('Please input your square matrix\n');
[nr1, nc1] = size(a1); % Creating a vector so I can check size of input
if nr1==nc1 % Checking the matrix is square
A = MyDet(a1); % Sending matrix to a different function
else
if nr1 ~= nc1 % Else for if matrix is not square
disp('The matrix is not square, please try again')
break % Break to stop the infinite loop
end
end
But now I don't know where to start with my new function MyDet. Any suggestions are helpful, thank you.
  2 Comments
Geoff Hayes
Geoff Hayes on 17 Nov 2014
Joshua - what method/algorithm have you discussed in class have for finding the determinant of a matrix?
Joshua
Joshua on 17 Nov 2014
For 2*2 and 3*3 we were just taught the patterns. For 4*4 we have used cofactor expansion. 5*5 and up I am actually not sure.

Sign in to comment.

Answers (1)

Matt J
Matt J on 17 Nov 2014
Edited: Matt J on 17 Nov 2014
Hint: Use cofactor expansion, calling MyDet recursively to compute the cofactors.
  3 Comments
Matt J
Matt J on 17 Nov 2014
When I say "call recursively", I mean that you can make MyDet call itself. To get the idea, you can study the example below. It is a function MySum, which calls itself recursively to obtain the sum of the elements x(i) in a vector x.
function out=MySum(x)
if isscalar(x)
out=x;
else
out=x(1)+MySum(x(2:end));
end
Matt J
Matt J on 17 Nov 2014
I thought because recursive functions create more functions they were an inefficient way to go about a task?
Even though recursion is often inefficient, this is a homework problem where you haven't been given any requirements on efficiency (that you've mentioned). If efficiency mattered, you would be told to use builtin functions.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!