如何从均值向量和 octave/matlab 中的 st.dev 生成多重随机分布?

How generate multiple random distribution from a vector of mean and st.dev in octave/matlab?

所以我有一个维度为 [5,1] 的向量 V。对于这个向量 V[i] 中的每个值,我想生成比方说 5 个正态分布的数字,均值 V[i] 和固定的 st 偏差。所以最后我将有一个矩阵 [5,5],它在第 i 行有 5 个值正态分布,均值 V[i]。我如何在不使用 for 循环的情况下使用 octave/matlab 执行此操作?实际上,我想将一个均值向量 V 传递给 normrnd 函数,并为向量 V 中的每个均值获得一组 n 个正态分布数。

对数组输入使用normrnd

你可以把均值向量转成矩阵传给normrnd。这是有效的,因为如 normrnd's documentation

中所述

r = normrnd(mu,sigma) [...]

To generate random numbers from multiple distributions, specify mu and sigma using arrays. If both mu and sigma are arrays, then the array sizes must be the same. If either mu or sigma is a scalar, then normrnd expands the scalar argument into a constant array of the same size as the other argument. Each element in r is the random number generated from the distribution specified by the corresponding elements in mu and sigma.

示例:

mu = [30; 15; 7; -60; 0]; % vector of means
std = 2;                  % common standard deviation
N = 4;                    % number of samples for each mean
result = normrnd(repmat(mu(:), 1, N), std);

使用带有隐式扩展的randn

您可以使用标准高斯分布的样本生成矩阵,乘以所需的标准差,然后将所需的均值添加到每一行:

result = mu(:) + std*randn(numel(mu), N);

之所以有效,是因为

  • 改变零均值高斯分布的标准差等同于缩放;
  • 改变高斯分布的均值等同于平移。

移动是使用 implicit expansion 完成的。这种方法避免了之前方法构建重复均值的中间矩阵,调用randn而不是normrnd,所以可能效率更高。