Prev: Workaround?
Next: AIC & BIC in GMM
From: Enosh Bale on 8 Mar 2010 21:01 that, given a square matrix, returns the sum of the diagonal elements.
From: Nathan on 8 Mar 2010 21:03 On Mar 8, 6:01 pm, "Enosh Bale" <fergysonnai...(a)gmail.com> wrote: > that, given a square matrix, returns the sum of the diagonal elements. doc sum doc diag Ex: >> A = magic(5) A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 >> diag(A) ans = 17 5 13 21 9 >> sum(diag(A)) ans = 65 -Nathan
From: Walter Roberson on 8 Mar 2010 22:05 Enosh Bale wrote: > that, given a square matrix, returns the sum of the diagonal elements. Create a new matrix B, which is the same size as the original matrix, A, but only has the diagonal elements of A. Calculate the determinant of B. The determinant will be the product of the eigenvalues, so factor the determinant to get the eigenvalues. The sum of the eigenvalues of a diagonal matrix is equal to the sum of the diagonal elements of the matrix, so sum the factors you found to get the desired result.
From: Matt Fig on 8 Mar 2010 22:09 Just another method, using linear indexing. % Given A is nxn n = 5; A = magic(n); % Linearly address the same elements as the DIAG function. sum(A(1:n+1:n^2))
From: Roger Stafford on 8 Mar 2010 22:46
"Enosh Bale" <fergysonnaiyep(a)gmail.com> wrote in message <hn4a5i$j1p$1(a)fred.mathworks.com>... > that, given a square matrix, returns the sum of the diagonal elements. There's a special matlab function to do just that operation on a square matrix. Remarkably enough, it is called "trace". Roger Stafford |