Azzera filtri
Azzera filtri

How I write a code calculating a matrix determinant recursively without using build-in operation?

49 visualizzazioni (ultimi 30 giorni)
Hello I get a project to do and I need to know how to write recursive matrix determinanent without using build-in operation?
  3 Commenti
Walter Roberson
Walter Roberson il 3 Lug 2022
https://en.m.wikipedia.org/wiki/Determinant#Calculation
When recursive calculation is asked for, that typically means that you are expected to do the calculation by The Method of Expansion Of Minors, which takes time proportional to factorial(n)
Omer Alon
Omer Alon il 3 Lug 2022
Well I actually try to do that with function that i found:
function det = myDet(A)
if isscalar(A)
det = A;
return
end
det = 0;
top_row = A(1,:);
A(1,:) = [];
for i = 1:size(A,2)
A_i = A;
A_i(:,i) = [];
det = det+(-1)^(i+1)*top_row(i)*myDet(A_i);
end
end
and then i compare it with built- in operators:
%create random 3X3 'a' matrix
a=randi(3,3)
determinant=0;
for i=1:3
% preserve the 'a' original matrix
A=a;
%delating the 1st row and column
A(1,:)=[];, A(:,i)=[];
%multyplying the 2x2 cofactor of A by the value of the appropriate row/column for any fixed i
d=((-1)^(1+i))*a(1,i)*myDet(A);
%summing each segment of the determinant
determinant=determinant+d;
end
determinant %show the our determinant calculation
det(a) %show the built-in matrix MATLAB operation determinant calculation
if determinant==round(det(a)) % checking if the calculations are the same for matlab built in determinant, using commened to not show it as a decimal number
'The determinant calculation is the same as the built-in matrix MATLAB operation'
else
'The determinant calculation is not the same as the built-in matrix MATLAB operation'
end

Accedi per commentare.

Risposte (1)

Gyan Vaibhav
Gyan Vaibhav il 8 Nov 2023
Hi Omer,
I understand that you want to write a recursive function that calculates the determinant without using the inbuilt MATLAB function, and then verify it using the inbuilt function.
The “myDet” function seems correct. It's a recursive function to calculate the determinant of a matrix using the Laplace expansion.
However, the comparison part has some issues. You're calculating the determinant twice: once using the “myDet” function and once using MATLAB's built-in det function. But in the first calculation, you're not using “myDet” directly on the matrix a, instead you're calculating the determinant inside a loop, which is unnecessary because “myDet” is already a recursive function.
Here is the modified code where I have removed the first loop where you were calculating the determinant manually. This is unnecessary as “myDet” function already does that.
Though you have used “randi” to generate the matrix, which only populates the matrix with integers. However, for the cases of floating-point numbers, I have replaced the equality check with a tolerance check. This is due to the nature of floating-point arithmetic, the two determinants might not be exactly equal, but they could be very close. Checking if their difference is less than a small tolerance value is a common way to compare floating point numbers.
%create random 3X3 'a' matrix
a=randi(3,3)
a = 3×3
1 3 1 2 3 1 3 1 2
% Calculate determinant using your function
determinant = myDet(a);
% Calculate determinant using built-in function
builtin_det = det(a);
% Print both determinants
disp(['Our calculation: ', num2str(determinant)]);
Our calculation: -5
disp(['MATLAB built-in calculation: ', num2str(builtin_det)]);
MATLAB built-in calculation: -5
% Compare the two results
if abs(determinant - builtin_det) < 1e-6 % Use a tolerance instead of equality check
disp('The determinant calculation is the same as the built-in matrix MATLAB operation')
else
disp('The determinant calculation is not the same as the built-in matrix MATLAB operation')
end
The determinant calculation is the same as the built-in matrix MATLAB operation
Below is the required function.
function det = myDet(A)
if isscalar(A)
det = A;
return
end
det = 0;
top_row = A(1,:);
A(1,:) = [];
for i = 1:size(A,2)
A_i = A;
A_i(:,i) = [];
det = det + (-1)^(i+1)*top_row(i)*myDet(A_i);
end
end
Hope this helps and resolves your issue.
Thanks
Gyan

Community Treasure Hunt

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

Start Hunting!

Translated by