View License

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

» Watch video

Highlights from
Hausdorff Distance

4.0 | 8 ratings Rate this file 17 Downloads (last 30 days) File Size: 1.55 KB File ID: #27905 Version: 1.0
image thumbnail

Hausdorff Distance



Computes the Hausdorff distance between two point clouds.

| Watch this File

File Information

This simple code computes the Hausdorff distance between two point clouds.
% Let A and B be subsets of a metric space (Z,dZ),
% The Hausdorff distance between A and B, denoted by dH (A, B), is defined by:
% dH (A, B)=max{sup dz(a,B), sup dz(b,A)}, for all a in A, b in B,
% dH(A, B) = max(h(A, B),h(B, A)),
% where h(A, B) = max(min(d(a, b))),
% and d(a, b) is a L2 norm.
% dist_H = hausdorff( A, B )
% A: First point sets.
% B: Second point sets.
% ** A and B may have different number of rows, but must have the same number of columns. **
% Hassan RADVAR-ESFAHLAN; Université du Québec; ÉTS; Montréal; CANADA
% 15.06.2010


This file inspired Modified Hausdorff Distance, *Mex* Modified Hausdorff Distance For 2 D Point Sets, and Hausdorff Distance.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
15 Dec 2016 Marta

Marta (view profile)

Sorry, forget the comment I have just pasted, I was looking at another piece of code...
It works great, many thanks.

15 Dec 2016 Marta

Marta (view profile)

Thank you for the submission.

Your code does not compute an L2-norm for the distances between the points as it is.

compute_dist should read at the end:
D = (C-B) .* (C-B);
D = sqrt(D.^2 * ones(dim,1));
dist(k) = min(D);

Without the D.^2 operation, this is a very unusual norm...

Comment only
18 Jul 2016 Anitha D

20 Mar 2016 ELI

ELI (view profile)

Good code, except that the variable "dist" in the function line suppose to be change to dH or vice versa. Otherwise it gives an error.

17 Dec 2015 Jan

Jan (view profile)

30 Aug 2011 Niki

Niki (view profile)

I have a Question, if I have two vectors,

is it possible to use of your M-file?

for example X1= x1=[1;2;3;4]
and x2=[3;6;4;1]
then error will appear

Error in ==> hausdorff at 16
if(size(A,2) ~= size(B,2))

Comment only
22 Nov 2010 Guilherme Mota

I 've tested the original code two times, before and after the modifications proposed in the previous comments. The results were identical. However, despite being minor changes, we felt more comfortable with the modified one. In our application, that is the final code of the Hausdorff function:

function [dH] = hausdorff( A, B)
% I 've changed dist to dH
if(size(A,2) ~= size(B,2))

fprintf( 'WARNING: dimensionality must be the same\n' );

dH = [];
% I 've changed dist to dH


dH = max(compute_dist(A, B), compute_dist(B, A))
; %I've included a semicolon

The rest was left unchanged.

I would like to express my gratitude to the author of this function Mr. Hassan Radvar-Esfahlan.

08 Jul 2010 Airballman

The update is not made yet, I had to change the code.
But it seems to be ok.

17 Jun 2010 John Sarkar

Good job. Thanks for sharing.

15 Jun 2010 Zachary Danziger

Its a good code.
The only (small) issue is that line 21 of the code is:
"dH = max(compute_dist(A, B), compute_dist(B, A))"
But the function is looking for the output called "dist". A small update to:
"dist = max(compute_dist(A, B), compute_dist(B, A));"
Without the update the code will not run and gives the error I mentioned. I'd be glad to change my rating to 5 stars after that very small change. :)

Comment only
15 Jun 2010 Hassan Radvar-Esfahlan

Dear Danziger
1) What you mean by crash? So, how you have verified the code?
2) I did not terminate with semicolon because I did not want to suppress the output.
3) The code is designed for n-dimensional space, but the previous version ( by: Zachary Danziger) works only with 2-dimensional data which is rarely occurs in real engineering applications.
Thanks for your evaluation.

Comment only
15 Jun 2010 Zachary Danziger

A few things:
1) Your code crashes:
"??? Output argument "dist" (and maybe others) not assigned during call..."
Line 21 should be changed to:
"dist = max(compute_dist(A, B), compute_dist(B, A));"
In order for the code to run and output supressed by the semicolon.

2) You don't ackgnoledge previous submissions:

3) Previous versions are vectorized and faster for "small" data sets:

P = randn(300,2);
Q = rand(50,2);
times = zeros(2,100);
for i=1:100
hd = hausdorff(P,Q);
times(1,i) = toc;
hd = HausdorffDist(P,Q);
times(2,i) = toc;

ans =

Contact us