Recursion in matrix calculation

7 visualizaciones (últimos 30 días)
StillANovice
StillANovice el 25 de Ag. de 2020
Editada: James Tursa el 25 de Ag. de 2020
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

Respuestas (1)

James Tursa
James Tursa el 25 de Ag. de 2020
Editada: James Tursa el 25 de Ag. de 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.

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by