【问题标题】:Generate a sparse row stochastic matrix?生成稀疏行随机矩阵?
【发布时间】:2014-02-08 21:30:50
【问题描述】:

我正在尝试使用 Matlab 生成一个稀疏随机矩阵,但目前遇到了问题。这是我目前所在的位置。

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); 

现在最后一行是失败的地方。稀疏矩阵中有一些零行。

所以我的问题是如何标准化每一行但保留零行?

【问题讨论】:

  • S1 = sparse (i,j,1,N,N); 行也会导致错误。 max(i) 和 max(j) 在某些情况下为 11,超过了 NxN 的大小。

标签: matlab matrix sparse-matrix stochastic


【解决方案1】:

这是我会使用的一个非常简单的模型:

%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);

【讨论】:

  • 谢谢。我会投票赞成你的答案,但我太新了。
  • 您始终可以通过单击复选标记accepting an answer。这表明您已经找到了解决方案,这不再是一个悬而未决的问题。
猜你喜欢
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多