Suppose I have an NxN matrix A, an index vector V consisting of a subset of the numbers 1:N, and a value K, and I want to do this:
for i = V A(i,i) = K
A = zeros(7,6); V = [1 3 5]; [n m] = size(A); diagIdx = 1:n+1:n*m; A( diagIdx(V) ) = 1 A = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0