0

How to define a structure input called mixture with the following fields: mu1, sigma1, mu1, sigma2, and wts in the following Matlab function:

   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 
2
  • What is the question? Commented Oct 28, 2015 at 16:23
  • How to define struct input called mixture with the specified fields to get the above script work. Commented Oct 28, 2015 at 16:25

1 Answer 1

1

I would first reference you to the manual entry on Matlab structures. But, the simplest thing to do would be to define it as:

mixture.mu1 = 1;
mixture.sigma1 = 1;
mixture.mu2 = 1;
mixture.sigma2 = 1;
mixture.wts = [1 1];

Of course, you'd enter the values you wanted for the fields of the structure.

A shorter way of doing the above would be:

mixture = struct('mu1', 1, 'mu2', 1, 'sigma1', 1, 'sigma2', 1, 'wts', [1 1]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.