gmmsamp.m

来自「Bayes网络工具箱」· M 代码 · 共 59 行

M
59
字号
function [data, label] = gmmsamp(mix, n)%GMMSAMP Sample from a Gaussian mixture distribution.%%	Description%%	DATA = GSAMP(MIX, N) generates a sample of size N from a Gaussian%	mixture distribution defined by the MIX data structure. The matrix X%	has N rows in which each row represents a MIX.NIN-dimensional sample%	vector.%%	[DATA, LABEL] = GMMSAMP(MIX, N) also returns a column vector of%	classes (as an index 1..N) LABEL.%%	See also%	GSAMP, GMM%%	Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997)% Check input argumentserrstring = consist(mix, 'gmm');if ~isempty(errstring)  error(errstring);endif n < 1  error('Number of data points must be positive')end% Determine number to sample from each componentpriors = rand(1, n);% Pre-allocate data arraydata = zeros(n, mix.nin);if nargout > 1  label = zeros(n, 1);endcum_prior = 0;		% Cumulative sum of priorstotal_samples = 0;	% Cumulative sum of number of sampled pointsfor i = 1:mix.ncentres  num_samples = sum(priors >= cum_prior & ...    priors < cum_prior + mix.priors(i));  % Form a full covariance matrix  switch mix.covar_type    case 'spherical'      covar = mix.covars(i) * eye(mix.nin);    case 'diag'      covar = diag(mix.covars(i,:));    case 'full'      covar = mix.covars(:,:,i);  end  data(total_samples+1:total_samples+num_samples, :) = ...    gsamp(mix.centres(i,:), covar, num_samples);  if nargout > 1    label(total_samples+1:total_samples+num_samples) = i;  end  cum_prior = cum_prior + mix.priors(i);  total_samples = total_samples + num_samples;end  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?