Matlab产生二维混合高斯分布及随机数

参考:https://ww2.mathworks.cn/help/stats/gmdistribution.random.html

修改如下:

Create a gmdistribution object and generate random variates.

Define the distribution parameters (means and covariances) of a two-component bivariate Gaussian mixture distribution.

p = [0.4 0.6]; % p is the proportion of two-component Gaussian distribution
mu = [1 2;-3 -5];
sigma = cat(3,[2 .5],[1 1]); % shared diagonal covariance matrix
sigma
sigma(:,:,1) =
    2.0000    0.5000
sigma(:,:,2) =
     1     1

Create a gmdistribution object by using the gmdistribution function. By default, the function creates an equal proportion mixture.

gm = gmdistribution(mu,sigma)
gm = 

Gaussian mixture distribution with 2 components in 2 dimensions
Component 1:
Mixing proportion: 0.400000
Mean:     1     2
Component 2:
Mixing proportion: 0.600000
Mean:    -3    -5

Generate 1000 random variates.

rng('default'); % For reproducibility
[Y,compIdx] = random(gm,1000);

The rng function can be set as a random seed like the rand().

compIdx(i) indicates the mixture component used to generate the ith random variate Y(i,:). Count the number of random variates generated by Component 1.

numIdx1 = sum(compIdx == 1)
numIdx1 = 405

random generates about half of the random variates using Component 1 because gm has equal mixing proportions.

Plot the generated random variates by using scatter.

scatter(Y(:,1),Y(:,2),10,'.') % Scatter plot with points of size 10

 

 

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值