File Exchange

image thumbnail


version 1.8 (20.6 KB) by

Fast implementation of EM algorithm for multivariate gaussian mixture

1 Download


View License

Mex implementation of EM algorithm for multivariate Gaussian mixture. Multiple data/initial parameters are allowed by ND slices definition

em_mvgm : Expectation-Maximization algorithm for Multivariate Gaussian Mixtures


[logl , M , S , P] = em_mvgm(Z , M0 , S0 , P0 , [nbite]);


Z Measurements (m x K x [n1] x ... x [nl])
M0 Initial mean vector. M0 can be (m x 1 x p x [v1] x ... x [vr])
S0 Initial covariance matrix. S0 can be (m x m x p x [v1] x ... x [vr])
P0 Initial mixture probabilities (1 x 1 x p) : P0 can be (1 x 1 x d x [v1] x ... x [vr])
nbite Number of iteration (default = 10)


logl Final loglikelihood (n1 x ... x nl x v1 x ... x vr)
M Estimated mean vector (d x 1 x p x n1 x ... x nl v1 x ... x vr)
S Estimated covariance vector (d x d x p x n1 x ... x nl v1 x ... x vr)
P Estimated initial probabilities (1 x 1 x p x n1 x ... x nl v1 x ... x vr)

Please run mexme_em_mvgm for compile mex file on your own systems.

Run test_em_mvgm.m for a demo

Comments and Ratings (7)


Works like charm after running the mex compilation code and the test program included. Will post more comments after checking the correctness of the program.



thank you very much for the fast reply!

Unfortunately I still get NaNs in all elements of the em_ghmm results (command: [logl , PIest , Aest , Mest , Sest] = em_ghmm(Ztrain , PI0 , A0 , M0 , S0 , options);).

Maybe these information are also useful:
I needed to add

#define max(a,b) ( (a) >= (b) ? (a) : (b) )

in some of the files (also in em_ghmm.c) to overcome the error

undefined reference to `max'

With this addition, everything compiles fine, with a warning when I compile ndellipse.c:

ndellipse.c:138: warning: assignment discards qualifiers from pointer target type
ndellipse.c:165: warning: assignment discards qualifiers from pointer target type
ndellipse.c:205: warning: assignment discards qualifiers from pointer target type

But as far I can see, the ndellipse function is not used until the first call of em_ghmm in the fest file test_em_ghmm.m.

Sebastien PARIS

Have you tried by recompiling as this script below ?

echo on
mex em_ghmm.c
mex -Dbetanormalize forward_backward.c
mex likelihood_mvgm.c
mex ndellipse.c
mex -DranSHR3 sample_ghmm.c
echo off

Never tried on 64 bits system


I am using R2008a on a 64bit machine under Linux. I made the files compile, but unfortunately, when I run the test_em_mvgm.m, I get NaN as results of the em_mvgm-function. Do I have to set specific options when compiling the files? Did anyone else have similar problems and solve this problem?

Sebastien PARIS

Please recompile the mex-File by following command :

mex -output em_mvgm.dll em_mvgm.c

Scott Miller

Hmm. Seems to crash my machine after the first pause with a minidump. I'm running 2007b

Dimitri Shvorob

How is your implementation different from
apart from yours being in C rather than Matlab?



-Fix 2 bugs (thanks to Jonathan for reporting)


-Fix a bug introduced in the last update


-Minor update for Linux


-Fix a compilation error for Linux and // comments


-Fixed a bug for Linux64 & GCC


Correct a bug in likelihood_mvgm.c when d=1


Fixed bug in the likelihood constant computation


- add mexme_em_mvgm to build mex files and compatible with LCC compiler.

MATLAB Release
MATLAB 7.5 (R2007b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video