How can I sum only few values in a diagonal matrix?
9 views (last 30 days)
Show older comments
if I have a diagonal matrix, and I use sum(diag(A)) , it gives me the sum of entire diagonal, but I only want the sum of lets say 1st three values in the diagonal? How can I add them?
Thank you!!
0 Comments
Accepted Answer
More Answers (2)
Alberto
on 14 Apr 2014
Sum=0;
for k=1:3 % will sum 3 first diagonal elements Sum=Sum+A(k,k);
end
You can change the range k=1:3 with an array with the index of elements u want to sum.
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!