Recursion in matrix calculation
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
How does recursion work in this context, as in how did the function CalDet manage to calculate the determinant of the minor without explicitly writing an equation?
function [determinant] = CalDet(M)
dimensionM = size(M);
if (dimensionM(1) == 1)
determinant = M(1, 1);
else
determinant = 0;
for i = 1:dimensionM(2)
determinant = determinant + (-1)^(i+1) * M(1, i) * CalDet(MMin(M, 1, i));
end
end
end
function [MatrixMinor] = MMin(M, i, j)
dimensionM = size(M);
MatrixMinor = M([1:(i-1) (i+1):dimensionM(1)], [1:(j-1) (j+1):dimensionM(2)]);
end
0 commentaires
Réponses (1)
James Tursa
le 25 Août 2020
Modifié(e) : James Tursa
le 25 Août 2020
This uses recursive calls (CalDet calls CalDet with smaller matrices until the size is 1x1). I.e., the recursion continues all the way down until the input is a 1x1 matrix, at which point the result is simply M(1,1) and then the results get passed back up through the stack of calls.
See Laplace's expansion and the adjugate matrix here:
BTW, this is not a good numerical technique.
0 commentaires
Voir également
Catégories
En savoir plus sur Desktop dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!