Use linear indexing:
A(sub2ind(size(A),b,b)) = 0;
This works for full or sparse matrices.
题
A quick question, I have a sparse matrix A and a index list b = [2, 3, 4 ...]
, I want to set A(2, 2), A(3, 3), A(4, 4)... = 0
. I tried A(b, b) = 0, the result is that A(2, 3), A(2, 4), A(3, 2), A(3, 4), A(4, 2), A(4, 3)
and some non-diagonal elements of this matrix are also set zero, how to deal with that?
解决方案