1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| figure(1) plot(x, y, 'o') hold on scatter1 = scatter(x,y,'MarkerFaceColor','k','MarkerEdgeColor','k'); scatter1.MarkerFaceAlpha = .3;
plot(m(:, 1), m(: ,2), 'o') scatter2=scatter(m(:, 1), m(: ,2), 'MarkerFaceColor','r','MarkerEdgeColor','r'); scatter2.MarkerFaceAlpha = .1;
[X, Y] = meshgrid(linspace(-10, 10, 100), linspace(-10, 10, 100));
for k = 1:3 p = mvnpdf([X(:) Y(:)], m(k, :), S(: ,:, k)) ; Z = reshape(p,size(X)); contour(X,Y,Z, 'LineWidth', 1.2) hold on end xlabel('$x_1$', "Interpreter","latex") ylabel('$x_2$', "Interpreter","latex") title('Mixture components') hold off
figure(2) plot(x, y, 'o') hold on scatter1 = scatter(x,y,'MarkerFaceColor','k','MarkerEdgeColor','k'); scatter1.MarkerFaceAlpha = .3;
plot(m(:, 1), m(: ,2), 'o') scatter2=scatter(m(:, 1), m(: ,2), 'MarkerFaceColor','r','MarkerEdgeColor','r'); scatter2.MarkerFaceAlpha = .1;
xlabel('$x_1$', "Interpreter","latex") ylabel('$x_2$', "Interpreter","latex") title('GMM')
[X, Y] = meshgrid(linspace(-10, 10, 100), linspace(-10, 10, 100)); gmm = zeros(100, 100) ; for k = 1:3 p = mvnpdf([X(:) Y(:)], m(k, :), S(: ,:, k)) ; gmm = gmm+ w(k).*reshape(p,size(X)); end contour(X,Y,gmm, 'LineWidth', 1.2) hold off
|