matlab - Matrix with at least one number in every row and column -


i'm trying build sparse matrix @ least 1 random number in every row , column.

in example form 4x4 random matrix sparsity 0.5. try go through rows, check if there zeros , add random number row random position if are. doesn't work, appreciate help.

a = sprand (4,4,0.5); = full (a); [n m] = size(a); x=1; = 1:n if any(a,2) == 0 j= randperm(m,x); a(i,j) = rand; end end 

sz=4; a=zeros(sz); %generating 1 random number in every row , column a(sub2ind([sz sz],randperm(sz),1:sz))=randn(sz,1);     %the following either generates no indices satisfying *at least* condition or %generates indices since there can more 1 rn in every row , column  ind = randi([1 sz*sz],1, randi([0 sz*sz],1,1));   a(ind)=randn(1,length(ind));    %more random numbers on generated indices 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -