How to assign the diagonal elements of part of matrix to a value?

You can simply do:

aa[diagind(aa)[5:7]] .= 1
5 Likes