在MATLAB中,如何根据数据生成随机数?

发布于 2024-10-05 19:54:25 字数 536 浏览 4 评论 0原文

例如,如果 Ax = 0.0023,它应该添加一些噪音并产生 newAx = 0.0027 如果Hy = 94.54,则newHy = 93.42。最初,我开始按以下方式在 MATLAB 中使用 rand,

newAx = Ax + rand
newAx = 0.9453

这与我拥有的参数 Ax(0.0023) 相差甚远。

如果我必须使用范围,那么在这种情况下我们必须使用 Ax 的最小值和最大值。这也带来了一些问题,比如它与价值观相去甚远。因为我有一个系统,其中 Ax 是在每个时刻逐渐增加的参数。现在,根据 Ax 的值,我必须在每个时刻生成一些随机噪声才能获得几乎接近 Ax 的 newAx 。例如,如果 Ax = 0.0023 生成 newAx = 0.0027 而不是 newAx = 0.4590

我需要创建一个取决于数据本身的随机噪声。

For example if Ax = 0.0023, it should add some noise to it and produce newAx = 0.0027
If Hy = 94.54,then newHy = 93.42. Initially I started using rand in MATLAB in the following manner,

newAx = Ax + rand
newAx = 0.9453

This is very much far from the parameter Ax(0.0023) which I have.

If I have to use range then in this case we have to use min and max of Ax. That also introduces issues like it is far from the values. As I have a system where Ax is a gradually increasing parameter at every instant of time. Now at every instant depending on the value of Ax I have to generate some random noise to get newAx which is almost close to Ax. Example if Ax = 0.0023 generate newAx = 0.0027 and not newAx = 0.4590

I need to create a random noise which depends on the data itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-10-12 19:54:25

一种方法是使用 RANDN 生成正态分布的噪声值,然后将噪声的标准差缩放为数据值的百分比:

noiseScale = 0.05;  %# Noise with a standard deviation of %5 of the data
newAx = Ax + noiseScale*Ax*randn;

如果 Ax 是要添加噪声的值的向量,您可以这样做:

newAx = Ax + noiseScale.*Ax.*randn(size(Ax));

如果您想确保 newAx 保持在特定的值范围内,您可以使用函数 MAXMIN< /a> 像这样:

newAx = min(newAx,maxValue);  %# Clip newAx to a maximum of maxValue
newAx = max(newAx,minValue);  %# Clip newAx to a minimum of minValue

One way to do this is to use RANDN to generate normally distributed noise values, then scale the standard deviation of the noise to be a percentage of the data value:

noiseScale = 0.05;  %# Noise with a standard deviation of %5 of the data
newAx = Ax + noiseScale*Ax*randn;

And if Ax is a vector of values that you want to add noise to, you can do this:

newAx = Ax + noiseScale.*Ax.*randn(size(Ax));

If you want to make sure newAx stays within a particular range of values, you can use the functions MAX and MIN like so:

newAx = min(newAx,maxValue);  %# Clip newAx to a maximum of maxValue
newAx = max(newAx,minValue);  %# Clip newAx to a minimum of minValue
梦醒灬来后我 2024-10-12 19:54:25
newDatum = oldDatum+2*(rand(1)-0.5)*oldDatum
newDatum = oldDatum+2*(rand(1)-0.5)*oldDatum
怎言笑 2024-10-12 19:54:25

您想模拟什么样的“噪音”?

rand(1) 给出 0 << 范围内的均匀随机数。 r < 1.“统一”表示每个值的可能性相等,就像滚动单个值一样,完美的死。

换句话说,随机值的范围为 +/- 0.5,以 0.5 为中心。您希望以实际值为中心,并且可能希望根据实际值缩放范围。例如

 2 * (rand(1) - 0.5) 

给你一个范围 -1 < 范围内的均匀随机数。 r < 1.


例如,如果噪声不应超过 1%,则需要使用:

newValue = oldValue * ( 1 +  2*(rand(1) - 0.5) * 0.01)

0.01 是我在示例中使用的比例 (1%),1 + ... 以原始值为中心。


许多进程没有均匀分布。随机噪声(例如传感器噪声)的常见分布是正态分布,其中值接近于中心的值比远离中心的值更有可能出现。

Matlab 支持 randn() 进行正态分布。

模拟其他系统的其他分布可以从均匀随机数导出。

What kind of "noise" are you trying to simulate?

rand(1) gives you a uniform random number in the range 0 < r < 1. "Uniform" means each value is equally likely, like rolling a single, perfect die.

In other words, the random value has a range of +/- 0.5, centered around 0.5. You want to center around your actual value, and you might want to scale the range depending on the actual value. e.g.

 2 * (rand(1) - 0.5) 

gives you a uniform random numbers in the range -1 < r < 1.


For example, if the noise should not exceed 1%, you would need to use:

newValue = oldValue * ( 1 +  2*(rand(1) - 0.5) * 0.01)

the 0.01 is the scale (1%) I used for the example, the 1 + ... centers around the original value.


Many processes don't have a uniform distribution. A common distribution for random noise (such as sensor noise) is a normal distribution, where values close to the center are more likely than values far from the center.

Matlab supports randn() for normal distribution.

Other distributions which model other systems can be derived from uniform random numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文