If you are going to use a vectorized method, you would need to get the linear indices to be set. The issue is that if you define your sparse matrix as K = sparse(N)
and then linearly index into K, it would extend the size of it in one direction only and not along both row and column. Thus, you need to specify to MATLAB that you are
looking to use this sparse to store a 2D array. Thus, it would be -
K = sparse(N,N);
Get the linear indices to index into K using sub2ind
and set them -
ind1 = sub2ind([N N],vectorofrandomintegers,vectorofrandomintegers);
K(ind1) = 1;