【问题标题】:Is there a better way to randomly generate a Doubly Stochastic Matrix?有没有更好的方法来随机生成双重随机矩阵?
【发布时间】:2012-11-17 09:00:24
【问题描述】:

这是电话n = 2*10^3; M = DStochMat02(n,ones(n)./n);的简介

 time   calls  line
                  1 function M = DStochMat02(n,c)
                  2 % Generate a random doubly stochastic matrix using
                  3 % Theorem (Birkhoff [1946], von Neumann [1953])
                  4 % Any doubly stochastic matrix M can be written as a convex combination
                  5 % of permutation matrices P1,...,Pk (i.e. M = c1*P1+...+ ck*Pk
                  6 % for nonnegative c1,...,ck with c1+...+ck = 1).
                  7 % Complexity: O(n^2)
                  8 % USE: M = DStochMat02(4,[1/2 1/8 1/8 1/4])
                  9 % Derek O'Connor, Oct 2006, Nov 2012. derekroconnor@eircom.net
  0.02       1   10 M = zeros(n,n);
< 0.01       1   11 I = eye(n);
< 0.01       1   12 for k = 1:n
   1.64   2000   13     pidx = GRPdur(n);                                 % Random Permutation
 107.72   2000   14     P = I(pidx,:);                                    % Random P matrix
  41.09   2000   15     M = M + c(k)*P;
< 0.01    2000   16 end


function p = GRPdur(n)
% -------------------------------------------------------------
% Generate a random permutation p(1:n) using Durstenfeld's
% Shuffle Algorithm, CACM, 1964.
% See Knuth, Section 3.4.2, TAOCP, Vol 2, 3rd Ed.
% Complexity: O(n)
% USE: p = GRPdur(10^7);
% Derek O'Connor, 8 Dec 2010.  derekroconnor@eircom.net
% -------------------------------------------------------------

    p = 1:n;                  % Start with Identity permutation
for k = n:-1:2
    r = 1+floor(rand*k);      % random integer between 1 and k
    t    = p(k);
    p(k) = p(r);               % Swap(p(r),p(k)).
    p(r) = t;
end
return % GRPdur

【问题讨论】:

  • 为什么不使用标准函数randperm 而不是GRPdur
  • @max taldykin 因为旧的 randperm 在时间和空间上效率低下。最新版本的 Matlab 使用了 Durstenfeld 算法的实现,该算法在时间和空间上都是最优的

标签: matlab matrix


【解决方案1】:

如何将1415 行更改为以下行:

l = ( [ pidx ; 1:n ] - 1 ) * [1;n] + 1; % convert pairs (pidx,1:n) to linear indices
M(l) = M(l) + c(k);

由于P 非常稀疏,也许只增加P 的非零值会更有效。

【讨论】:

  • 看准了!这非常棒,与Matt Fighere给出的方法相同:mathworks.co.uk/matlabcentral/answers/…它比我的matlab函数快300倍。
  • @DerekO'Connor 我还没有看到 mathworks 解决方案 - 很高兴知道我的 matlab 是最新的。谢谢!
猜你喜欢
  • 2019-10-29
  • 2021-05-09
  • 2019-08-23
  • 2016-06-30
  • 2022-01-07
  • 2013-03-26
  • 2018-10-26
  • 2012-11-21
  • 2018-08-11
相关资源
最近更新 更多