生成具有指定标准差和平均值的随机数数组

问题描述 投票:0回答:1

我想通过

N
双精度数组创建一个
1
,其中
N
是任何整数值。数组中的每个值应使用标准差
n
和平均值
n/10
偏离标称值
0
n
可以是任何整数或双精度值。

有没有办法在 MATLAB 中使用

randn(),normrnd,
或其他一些随机数函数来执行此操作,而无需使用 for 循环?

arrays matlab random mean standard-deviation
1个回答
1
投票

我认为这应该可以解决您的问题:

N = 10; 
n = 5;
standard_deviation = n / 10;

% Generates a vector of N ones, then moltiply it to the scalar n
nominal_values = n * ones(N, 1);

% Generates a vector of N standard random normal realizations, then multiply them to the scalar (n/10) -> you get a centered random normal with std n/10
deviations = standard_deviation * randn(N, 1);

% Get the final result
result_array = nominal_values + deviations;

我希望这能解决您的问题,如果需要,欢迎您要求澄清。

© www.soinside.com 2019 - 2024. All rights reserved.