MATLAB: Simple Matrix manipulation without for-loop needed

for loopMATLABmatrix manipulation

Hello Community,
i need your help.
I have a matrix which size is (N+1,N+1). I have also some vectors with different length. The elements of these vectors contain integer numbers.
The code above is modifying the matrix L in such a way, that it reads at first the first elemtn of vector vec and it set the row with the index = vec(i) to zero. and set a 1 on L(index,index).
This is accomblished by a slow for loop.
Do you have any ideas how it could be done in a more faster and elegent way?
function L = modifyLDirichlet(L,vec)
for i=1:numel(vec)
L(vec(i),:) = 0;
L(vec(i),vec(i)) = 1;
end
end
Thank you for your advice!
Best,
MJ

Best Answer

L(vec,:) = 0;
L(sub2ind(size(L),vec,vec)) = 1;