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
I'd use sub2ind and pass the diagonal indices as both x and y parameters:
sub2ind
A = zeros(4) V=[2 4] idx = sub2ind(size(A), V,V) % idx = [6, 16] A(idx) = 1 % A = % 0 0 0 0 % 0 1 0 0 % 0 0 0 0 % 0 0 0 1