M =[1 2 3 4; 5 6 7 8; 9 0 1 2; 3 4 5 6]
LD = sum(spdiags(M(end:-1:1,:),1-size(M,1):size(M,2)-1))
RD = sum(spdiags(M',1-size(M,2):size(M,1)-1))
variant 2 without spdiags
M =[1 2 3 4; 5 6 7 8; 9 0 1 2; 3 4 5 6]
[m n] = size(M);
Mi = cumsum([1:n*2-1;(m-1)*ones(m-1,n*2-1)]);
LM = triu(true(size(Mi)));
LM = LM&LM(end:-1:1,end:-1:1);
M1 = M(Mi);
LD = sum(M1.*LM);
M2 = M(:,end:-1:1);
M2 = M2(Mi);
RD = sum(M2.*LM);
variant 3 with bsxfun
m = size(M,1);
idx = bsxfun(@plus,[1:m-1, m:m:numel(M)],(0:m-1:(m-1)^2)');
L = triu(true(size(idx)));
L1 = L&L(:,end:-1:1);
idx(~L1) = 1;
LD = sum(M(idx).*L1);
M1 = M(:,end:-1:1);
RD = sum(M1(idx).*L1);
variant 4 with convmtx from Signal Processing Toolbox
[m n]= size(M);
L = logical(convmtx(ones(n,1),m));
idx = ones(size(L));
idx(L) = 1:m*n;
LD = sum((M(idx).*L)');
M1 = M(:,end:-1:1);
RD = sum((M1(idx).*L)');
Best Answer