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:
Matix maths

Subject: Matix maths

From: Daniel Robbins

Date: 15 Nov, 2012 20:09:17

Message: 1 of 7

Does anyone know how I can sum all the diagonals of a matrix and get a vector of the results?

For example if a had the following matrix

18 36 24 12
12 24 16 8
 6 12 8 4

I would get a vector of:

18 48 54 40 16 4

All help appreciated!
Dan

Subject: Matix maths

From: dpb

Date: 15 Nov, 2012 20:25:13

Message: 2 of 7

On 11/15/2012 2:09 PM, Daniel Robbins wrote:
> Does anyone know how I can sum all the diagonals of a matrix and get a
> vector of the results?
> For example if a had the following matrix
> 18 36 24 12
> 12 24 16 8
> 6 12 8 4
>
> I would get a vector of:
>
> 18 48 54 40 16 4
>

Not terribly clever, but...given your array as 'm'--

 >> n=fliplr(m)
n =
     12 24 36 18
      8 16 24 12
      4 8 12 6
 >> [nr,nc]=size(n);d=nc-1;for i=1:nr+nc-1,sum(diag(n,d)),d=d-1;end
ans =
     18
ans =
     48
ans =
     54
ans =
     40
ans =
     16
ans =
      4
 >>

--

Subject: Matix maths

From: Nasser M. Abbasi

Date: 15 Nov, 2012 20:35:50

Message: 3 of 7

On 11/15/2012 2:09 PM, Daniel Robbins wrote:
> Does anyone know how I can sum all the diagonals of a matrix and get a vector of the results?
>
> For example if a had the following matrix
>
> 18 36 24 12
> 12 24 16 8
> 6 12 8 4
>
> I would get a vector of:
>
> 18 48 54 40 16 4
>
> All help appreciated!
> Dan
>

completley not clear question. Which diagonals? positive slope or
negative slope? which sub matrices are you summing its diagonals?

I can see where the '18' and the '48' and the '54' came from. But
the rest is not clear now how you got it.

Subject: Matix maths

From: Roger Stafford

Date: 15 Nov, 2012 21:57:11

Message: 4 of 7

"Daniel Robbins" <d.w.e.robbins@gmail.com> wrote in message <k83i5d$rqg$1@newscl01ah.mathworks.com>...
> Does anyone know how I can sum all the diagonals of a matrix and get a vector of the results?
> For example if a had the following matrix
>
> 18 36 24 12
> 12 24 16 8
> 6 12 8 4
>
> I would get a vector of:
>
> 18 48 54 40 16 4
- - - - - - - - -
 Let A be your matrix. Then v will be your desired vector:

 [m,n] = size(A);
 B = [zeros(m,m-1),A,zeros(m,m-1)];
 v = sum(B(bsxfun(@plus,(0:m-1:(m-1)^2)',m:m:m*(m+n-1))),1);

Roger Stafford

Subject: Matix maths

From: Bruno Luong

Date: 15 Nov, 2012 22:49:21

Message: 5 of 7

A=[18 36 24 12
12 24 16 8
6 12 8 4]

[m n]=size(A);
[I J]=ndgrid(1:m,1:n);
accumarray(I(:)+J(:)-1,A(:))

% Bruno

Subject: Matix maths

From: Bruno Luong

Date: 15 Nov, 2012 22:59:16

Message: 6 of 7

[m n]=size(A);
K=hankel(1:m,m+(0:n-1))
accumarray(K(:),A(:))

% Bruno

Subject: Matix maths

From: Daniel Robbins

Date: 16 Nov, 2012 09:50:10

Message: 7 of 7

Thanks to everyone who gave solutions, good to have a range of options to pick from!!

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