Reputation: 1
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?
Upvotes: 0
Views: 823
Reputation: 36720
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);
Upvotes: 1