MATLAB: How to sum only few values in a diagonal matrix diagonal 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!! Best Answer Rida, use sum(diag(A(1:3,1:3))) Related SolutionsMATLAB: How to Sum matrix diagonals sum(diag(a(:,end:-1:1)));or summing all diagonals:sum(spdiags(rot90(a))); MATLAB: Calculations or sum all diagonal elements FirstN = @(V,N) V(1:N);LastN = @(V,N) V(end-N+1:end);sum(FirstN(diag(a), 170))sum(LastN(diag(a), 80)) Related QuestionN number of 2×2 matricies to diagonal matrix
Best Answer