E.g., for generic x
s = sub2ind(size(d),x(1:end-1),x(2:end));
Dsum = sum(d(s));
If you always wanted to sum the super-diagonal, then
Dsum = sum(diag(d,1));
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!