Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
given matrix, sum of diagonal elements

Subject: given matrix, sum of diagonal elements

From: Enosh Bale

Date: 9 Mar, 2010 02:01:22

Message: 1 of 9

that, given a square matrix, returns the sum of the diagonal elements.

Subject: given matrix, sum of diagonal elements

From: Nathan

Date: 9 Mar, 2010 02:03:48

Message: 2 of 9

On Mar 8, 6:01 pm, "Enosh Bale" <fergysonnai...@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

Subject: given matrix, sum of diagonal elements

From: Walter Roberson

Date: 9 Mar, 2010 03:05:56

Message: 3 of 9

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.

Subject: given matrix, sum of diagonal elements

From: Matt Fig

Date: 9 Mar, 2010 03:09:05

Message: 4 of 9

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))

Subject: given matrix, sum of diagonal elements

From: Roger Stafford

Date: 9 Mar, 2010 03:46:06

Message: 5 of 9

"Enosh Bale" <fergysonnaiyep@gmail.com> wrote in message <hn4a5i$j1p$1@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

Subject: given matrix, sum of diagonal elements

From: Matt Fig

Date: 9 Mar, 2010 03:52:05

Message: 6 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message
> There's a special matlab function to do just that operation on a square matrix. Remarkably enough, it is called "trace".
>
> Roger Stafford

D'oh. I am not sure I've ever even seen the TRACE function!
I do note that it is an M-File which uses:

t = sum(diag(a));

Subject: given matrix, sum of diagonal elements

From: Jos (10584)

Date: 9 Mar, 2010 12:54:03

Message: 7 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message

Hi Roger,

It's good to see you again, after such a long break!
Did, by any chance, your email address change?

Jos

http://www.mathworks.com/matlabcentral/fileexchange/authors/10584

Subject: given matrix, sum of diagonal elements

From: Jos (10584)

Date: 9 Mar, 2010 12:57:03

Message: 8 of 9

"Enosh Bale" <fergysonnaiyep@gmail.com> wrote in message <hn4a5i$j1p$1@fred.mathworks.com>...
> that, given a square matrix, returns the sum of the diagonal elements.

without using sum, diag(onal), or even TRACE :

ones(1,min(size(A))) * A(eye(size(A))==1)

Jos

Subject: given matrix, sum of diagonal elements

From: Husam Aldahiyat

Date: 9 Mar, 2010 13:30:21

Message: 9 of 9

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hn5giv$89f$1@fred.mathworks.com>...
> "Enosh Bale" <fergysonnaiyep@gmail.com> wrote in message <hn4a5i$j1p$1@fred.mathworks.com>...
> > that, given a square matrix, returns the sum of the diagonal elements.
>
> without using sum, diag(onal), or even TRACE :
>
> ones(1,min(size(A))) * A(eye(size(A))==1)
>
> Jos

I like:

>> sum(A(1:size(A)+1:numel(A)))

Or without sum version:

>> A(1:size(A)+1:numel(A))*ones(length(A),1)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us