"Daniel Robbins" <
d.w.e....@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