gm_generation.m
来自「EM算法」· M 代码 · 共 49 行
M
49 行
%*********************************************************************
% Function objective: Generate clusters as specified
% Parameter Explanations:
% Inputs:
% k: the dimension of observations x's.
% onum: the total number of observations.
% range: the cumulative prior proportion of each cluster.
% mu: the means of the clusters.
% sigma: the covariance matrix of the clusters.
% Output:
% x: the observations forming clusters as specified.
%*********************************************************************
function [x] = gm_generation(k, onum, range, mu, sigma)
randn('seed', 65536);
rand('seed', 65530);
sn = randn(k,onum);
% number of mixture-of-Gaussian distribution
mix_no = size(range,2);
t = zeros(1,onum);
x = zeros(k, onum);
for i = 1:onum
r = rand;
for j = 1:mix_no
if r <= range(j)
t(i) = j;
break;
end
end
end
sqrt_sigma = zeros(size(sigma));
% Calculate the square root of a matrix
for j = 1:mix_no
sqrt_sigma(:,:,j) = sqrtm(sigma(:,:,j));
end
% Generate the observations
for i=1:onum
x(:,i)= sqrt_sigma(:,:,t(i))*sn(:,i)+mu(:,t(i));
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?