【问题标题】:MATLAB Normal random numbers generation for array of means without loop无循环均值数组的 MATLAB 正态随机数生成
【发布时间】:2016-09-08 17:52:28
【问题描述】:
mu=[1 2 3 4 5]';   
sigma=[1 1 1 1 1]';
N=100;    %Number of samples for each mu
R=normrnd(mu,sigma,?)

使用 normrnd,是否可以在没有循环的情况下为每个 mu 值生成 N 个样本(这样 R 将是 5 x 100 矩阵)?

【问题讨论】:

    标签: matlab loops random-sample


    【解决方案1】:

    我不知道规范。

    对于较旧的 randn,我会使用类似的东西:

    repmat(mu,N,1) + randn(N,length(mu))*diag(sigma)
    

    编辑

    啊,你想要转置 5x100,它是

    repmat(mu,1,N) + diag(sigma)*randn(length(mu),N)
    

    【讨论】:

    • 那行得通。如果我可以提出建议,请考虑使用bsxfun 而不是repmatbsxfun(@plus, mu, randn(N, numel(mu))*diag(sigma)); 您可以在此处查看性能比较的综合调查:stackoverflow.com/questions/29719674/… - 通常bsxfun 比使用等效的更快、更有效带有repmat的代码。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2010-12-25
    • 1970-01-01
    相关资源
    最近更新 更多