Best way to calculate the determinants of a series of matrices?
Mostrar comentarios más antiguos
I've got a series of time-dependent matrices
which I'd like to calculate the determinants
of. These matrices are stored as a three-dimensional array, where the first dimension indicates the period; in other words, the matrix
at time t is given by
Gt = squeeze(G(t, :, :))
I'd now like a snippet of code which, being run, will ensure that
Delta(t) = det(squeeze(G(t, :, :)))
holds for all t. Of course I could do this with a loop, but I feel that there must be a more succinct, vectorized way of doing it. Sadly, MATLAB's det function itself is of no help. Is there something else I could use, or will I have to bite the proverbial bullet and use a loop after all?
Respuesta aceptada
Más respuestas (3)
Alex Sune
el 5 de Sept. de 2019
delta = arrayfun(@(t) det(squeeze(G(t,:,:))),1:size(G,1));
1 comentario
Christian Schröder
el 5 de Sept. de 2019
Fabio Freschi
el 5 de Sept. de 2019
Not sure if it you can speedup your code, but a single line code to do the job is
Delta = arrayfun(@(i)det(squeeze(G(i,:,:))),1:size(G,1));
1 comentario
Christian Schröder
el 5 de Sept. de 2019
Jos (10584)
el 5 de Sept. de 2019
Elaborating on the answers using arrayfun, you can avoid the multiple squeeze operations by permuting the dimension order first:
G = permute(G,[2 3 1]) ; % last dimension is running fastest
D = arrayfun(@(k) det(G(:,:,k)), 1:size(G,3)) % per Fabio and Alex
1 comentario
Christian Schröder
el 5 de Sept. de 2019
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!