Got Questions? Get Answers.
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:
cross diagonal enteries

Subject: cross diagonal enteries

From: Kashif

Date: 12 Aug, 2010 17:18:04

Message: 1 of 5

Hi All,

I need to find the sum of cross diagonal entries.

If A =[x11 , x12, x13;
                    x21 , x22, x23;
                    x31 , x32, x33];

then I need the sum of x13,x22,x31.
I need to do this for a general N by N matrix.

Best Regards.

Subject: cross diagonal enteries

From: Andy

Date: 12 Aug, 2010 17:25:20

Message: 2 of 5

"Kashif " <rajakashif@gmail.com> wrote in message <i41acc$egs$1@fred.mathworks.com>...
> Hi All,
>
> I need to find the sum of cross diagonal entries.
>
> If A =[x11 , x12, x13;
> x21 , x22, x23;
> x31 , x32, x33];
>
> then I need the sum of x13,x22,x31.
> I need to do this for a general N by N matrix.
>
> Best Regards.

A=magic(5); % sample data
[n, n] = size(A); % we know A is square
antidiagonal = 0;

for ix = 1:n
  antidiagonal = antidiagonal + A(ix, n-ix+1);
end

Subject: cross diagonal enteries

From: ImageAnalyst

Date: 12 Aug, 2010 17:46:36

Message: 3 of 5

% Setup/initialization
A=magic(6) % Sample data
[rows columns] = size(A) % Get size of array.
linearIndices = columns:(columns-1):rows*columns-1 % Just for your
info, or you can plug in below.

% Here's the answer:
sumAntiDiagonal = sum(A(columns:(columns-1):end-1))

Subject: cross diagonal enteries

From: Walter Roberson

Date: 12 Aug, 2010 17:54:33

Message: 4 of 5

Kashif wrote:

> I need to find the sum of cross diagonal entries.
>
> If A =[x11 , x12, x13; x21 , x22, x23;
> x31 , x32, x33];
>
> then I need the sum of x13,x22,x31.
> I need to do this for a general N by N matrix.

N = length(A);
sum(A(N:N-1:end-N+1))

Subject: cross diagonal enteries

From: Roger Stafford

Date: 12 Aug, 2010 17:58:04

Message: 5 of 5

"Kashif " <rajakashif@gmail.com> wrote in message <i41acc$egs$1@fred.mathworks.com>...
> Hi All,
>
> I need to find the sum of cross diagonal entries.
>
> If A =[x11 , x12, x13;
> x21 , x22, x23;
> x31 , x32, x33];
>
> then I need the sum of x13,x22,x31.
> I need to do this for a general N by N matrix.
>
> Best Regards.
- - - - - - - -
  Or

 trace(fliplr(A))

Roger Stafford

Tags for 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