indexing diagonal elements of a matrix in a loop
Show older comments
I have an n x n matrix, some of its diagonals are zeros. I need to eliminate the rows and columns corresponding to the diagonal zeros in a loop and retain all other elements. ie, if A=[0 3 5 6 5;3 0 4 7 8;7 9 1 0 3;2 4 6 0 8;1 4 5 7 6],after elimination of rows and columns corresponding to diagonal zeros the answer should be, ans=[1 3;5 6]
anyone please help with appropriate code.
Accepted Answer
More Answers (1)
Andrei Bobrov
on 2 Jun 2013
ii = find(diag(A));
out = A(ii,ii);
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!