简体   繁体   中英

set some diagonal elements of a matrix in Matlab

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?

Use linear indexing :

A(sub2ind(size(A),b,b)) = 0;

This works for full or sparse matrices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM