0

I'm trying to generate a sparse stochastic matrix with Matlab but currently running into problems. Here is where I'm currently at.

N=10
i = round(rand(1,N)*10)+1 
j = round(rand(1,N)*10)+1

S1 = sparse (i,j,1,N,N);
S = full(S1)

rowsum = sum(S,2); 
S = bsxfun(@rdivide, S, rowsum); 

Now this last line is where it fails. There are some zero rows in the sparse matrix.

So my question is how can I normalize each row yet preserve the zero rows?

  • The line `S1 = sparse (i,j,1,N,N);` causes errors as well. max(i) and max(j) is 11 in some cases, which exceeds the size of NxN. – Daniel Feb 08 '14 at 21:58

1 Answers1

1

This is a very simple model i would use:

%logical matrix, a web page links to 20% of the other websites on average. This is a strange model, but I don't have a better idear:
doeslink=rand(N)<.2.*1-eye(N)
%generate random link weights
S=rand(N).*doeslink
%avoid nans
rowsum(rowsum==0)=1
%normalise
S = bsxfun(@rdivide, S, rowsum);
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Thank you. I would vote up your answer but I'm too new. – user3288167 Feb 08 '14 at 23:52
  • You can always [accepting an answer](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates that you've found a solution and this is no longer an open question. – Daniel Feb 17 '16 at 12:33