I'm trying to produce a sparse matrix that has zeros on the diagonal, but also has at least one element in each row and column.
With this code I get the first part, but I don't know how to make sure that each row and column have elements in them.
R = sprand(4,4,0.5);r = 0 + (0.5 - 0)*rand(4,4);A = full (R);A(1:4+1:4*4)=0;Nonz = nnz(A);Diff = ceil(0.5*(4^2-4)) - Nonz;B = find (A==0);A=A';B = find (A==0);C = setdiff(B,1:4+1:4*4);D = datasample(C,Diff,1,'Replace',false);E=A;for i = 1:length(D) E(D(i)) = 1;endE=E';E(find(E)) = r(find(E));
Best Answer