File Exchange

image thumbnail

EMD (Earth Movers Distance) mex interface

version (14.5 KB) by Masoud Alipour
emd_mex computes Earth Movers Distance between two signatures.


Updated 15 Dec 2006

No License

This code is the mex interface for computing Earth Movers Distance between signatures and is based on the Y. Rubner's C Implementation.

For building the mex file you should have setup your mex build environment. Invoke "build_emd" for building and "demo_emd" for a small test.

Cite As

Masoud Alipour (2021). EMD (Earth Movers Distance) mex interface (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Eirik Kvernevik

I want to add one more comment here : is the cost matrix C equvalent with the Ground distance matrix ? defined as the distance between features in the two distributions ?

Eirik Kvernevik

hi, as Abdur Rahaman mentioned in the comment below we would appreciate a more precise description on how to compute the cost matrix C.
in My case I use the midpoints of each histogram bin as features for my signatures.
Please give me some instructions on how to compute C.

Md Abdur Rahaman

How to compute C? I would appreciate if you can describe a little more precisely.

wei tong


Pol del Aguila Pla

Ubuntu 17.10 64 bit with 2017a, with modifications by Patrick. Ignored the gcc message when running build_emd.m as-is, and everything is working fine for the simple example. I'll report further if this changes for full-scale use.

Ming Cai

Shigeru Kitazawa

The code worked really well on Windows 10. I'm now keen to run the code on Ubuntu 16.0.4 (64 bit) with 2017a and modified the code as suggested by Patrick. Further , I had to install gcc v4.9 and modify the build_emd.m as follows:
mex -v GCC='usr/bin/gcc-4.9' emd_mex.c emd.c -O
The build was successful to yield emd_mex.mexa64. However, running the demo_emd has ended up in "core dump" with termination of Matlab. Does anybody advise me how to debug the mex file?

tigor pakpahan


Thanks, Masoud!

I have a 64 bit Linux system. To get this code to work, I needed to make the following modifications.

1. In emd_mex.c line 106: change "//maxsimum size of flow" to "/*maxsimum size of flow*/" since the "//" for single-line C comments are Microsoft only.

2. Add a line break to the end of emd_mex.c and save.

3. Run build_emd.

Thanks, and I hope that was helpful.


Csaba Beleznai

Michaels question is justified. The features (bin locations) are computed within the mex as consecutive indices. Thus, for a complete definition of a signature, you have to include the full span of weights for all bins (which is cumbersome if working with multidimensional signatures).

Rajat Sharma

Works really well. Thank you for doing this.

Michael Blighe

Where are the actual features (signatures) defined? The demo only uses the weights and the ground distance matrix?

Amir Anvari

Al Fa

Great, works perfect!!!

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!