如何使用以下字段定义名为mixture的结构输入:mu1,sigma1,mu1,sigma2和wts,如下Matlab函数:
function plotMixtureGaussians( mixture )
% determine the contour described by the mixture
m1 = mixture.mu1';
s1 = mixture.sigma1;
m2 = mixture.mu2';
s2 = mixture.sigma2;
w1 = mixture.wts(1);
w2 = mixture.wts(2);
xlim = -5:0.1:5;
ylim = -5:0.1:5;
[x,y] = meshgrid( xlim, ylim );
z = zeros( length(xlim), length(ylim) );
for ii = 1:length(xlim)
for jj = 1:length(ylim)
X = [x(ii,jj), y(ii,jj)]';
t1 = w1 * gaussian2D( X, m1, s1 );
t2 = w2 * gaussian2D( X, m2, s2 );
if t1 > t2
z(ii,jj) = 1;
else
z(ii,jj) = -1;
end
end
end
contour(x,y,z, 'XData', xlim, 'YData', ylim)
hold on;
% plot the means
x = [ mixture.mu1(1) mixture.mu2(1)];
y = [ mixture.mu1(2) mixture.mu2(2)];
plot( x, y, 'r.', 'Markersize', 5);
% plot the individual gaussians
plotGaussian( m1, s1, xlim, ylim );
plotGaussian( m2, s2, xlim, ylim );
end
function p = gaussian2D( X, mu, s )
p = (1/( 2 *pi *det(s)^2) * exp(-0.5 * (X - mu)' * (s \ (X-mu)) ));
end
function plotGaussian( mu, s, xlim, ylim )
hold on
[x,y] = meshgrid( xlim, ylim );
z = zeros( length(xlim), length(ylim) );
for ii = 1:length(xlim)
for jj = 1:length(ylim)
X = [x(ii,jj), y(ii,jj)]';
z(ii,jj) = gaussian2D( X, mu, s );
end
end
contour(x,y,z, 'XData', xlim, 'YData', ylim);
end
答案 0 :(得分:1)
我首先会引用您Matlab structures上的手册输入。但是,最简单的方法是将其定义为:
mixture.mu1 = 1;
mixture.sigma1 = 1;
mixture.mu2 = 1;
mixture.sigma2 = 1;
mixture.wts = [1 1];
当然,您可以为结构的字段输入所需的值。
执行上述操作的较短方式是:
mixture = struct('mu1', 1, 'mu2', 1, 'sigma1', 1, 'sigma2', 1, 'wts', [1 1]);