[Y,compIdx] = random(___)
also returns an n-by-1 index vector compIdx
for any of the input arguments in previous syntaxes. compIdx(i)
indicates the mixture component used to generate the ith random
variate Y(i,:).
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.500000
Mean: 1 2
Component 2:
Mixing proportion: 0.500000
Mean: -3 -5
Generate 1000 random variates.
rng('default'); % For reproducibility
[Y,compIdx] = random(gm,1000);
compIdx(i) indicates the mixture component used to generate the ith random variate Y(i,:). Count the number of random variates generated by Component1.
numIdx1 = sum(compIdx == 1)
numIdx1 =
512
random generates about half of the random variates using Component1 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
The cat function concatenates the covariances along the third array dimension. The defined covariance matrices are diagonal matrices. sigma(1,:,i) contains the diagonal elements of the covariance matrix of component i.
Create a gmdistribution object by using the gmdistribution function.
gm = gmdistribution(mu,sigma);
Save the current state of the random number generator, and then generate a random variate using gm.
s = rng;
r = random(gm)
r = 1×2
-1.1661 -7.2588
Restore the state of the random number generator to s, and then generate a random variate using gm. The values are the same as before.
gm — Gaussian mixture distribution gmdistribution object
Gaussian mixture distribution, also called Gaussian mixture model (GMM), specified as a gmdistribution object.
You can create a gmdistribution object using gmdistribution or fitgmdist. Use the gmdistribution function to create a
gmdistribution object by specifying the distribution parameters.
Use the fitgmdist function to fit a gmdistribution
model to data given a fixed number of components.
n — Number of random variates 1 (default) | positive integer
Number of random variates to generate, specified as a positive
integer.
Y — Random variate 1-by-m numeric vector | n-by-m numeric matrix
Random variate, returned as a 1-by-m numeric vector or
an n-by-m numeric matrix. Each row
of Y is a random variate generated from the
m-dimensional Gaussian mixture distribution
gm.
compIdx — Component index positive integer | n-by-1 numeric vector
Component index, returned as a positive integer or an
n-by-1 index vector, where
compIdx(i) indicates the mixture component used to
generate the ith random variate
Y(i,:).
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.