MATLAB – Calculate L2 Euclidean distance

Here’s how to calculate the L2 Euclidean distance between points in MATLAB.

The whole kicker is you can simply use the built-in MATLAB function, pdist2(p1, p2, ‘euclidean’) and be done with it. p1 is a matrix of points and p2 is another matrix of points (or they can be a single point).

However, initially I wasn’t really clear about what was going on. So if you are still a bit confused, let’s chat about it…

The scenario: You have one point, aPoint, that you wish to compare against a bunch of other points bunchOfPoints. For simplicity, let’s work in two-dimensional (2D) space. Note that 2D means each point is composed of two pieces of information (i.e. has 2 components).

We start by defining our points, then we calculate the L2 distance by hand, then we use the built-in pdist2() function to show we get the same result. And then finally, as a little bonus, we show how to get the minimum L2 Euclidean distance at the end.

Here’s how to calculate the equation by hand if you’re interested.
http://en.wikipedia.org/wiki/Norm_%28mathematics%29#Euclidean_norm

% disL2
 
% Define our points.
aPoint = [1,4]; % A single point with 2 components.
bunchOfPoints = [2,3; 1,4; 0,1]; % A bunch of other points.
 
% Make 'aPoint' the same size as a 'bunchOfPoints'.
aPointMatrix = repmat(aPoint,size(bunchOfPoints,1),1);
 
% Calculate by hand.
%% L2 Euclidean Norm.
% http://en.wikipedia.org/wiki/Norm_%28mathematics%29#Euclidean_norm
% 1) Take the difference between the two -> aPointMatrix-bunchOfPoints
% 2) Square the difference to get rid of positive/negative: 
%       (aPointMatrix-bunchOfPoints).^2
% 3) Sum this up along the rows.
%       (sum(((aPointMatrix-bunchOfPoints).^2), 2))
% 4) Take the square root of this.
%       (sum(((aPointMatrix-bunchOfPoints).^2), 2)).^0.5
pointsDifSquare = (sum(((aPointMatrix-bunchOfPoints).^2), 2)).^0.5
 
% output = 1.4142 0 3.1623
 
%% Or we can just use this handy built-in function...
d = pdist2(aPoint,bunchOfPoints,'euclidean') 
 
% same output! = 1.4142 0 3.1623
 
%% Bonus, how to find the min distance!
[theMinDistance, indexOftheMinDistance] = min(d)
 
% Yeah! As expected theMinDistance = 0 and indexOftheMinDistance = 2.

MATLAB – how to make a movie of plots

Making a video of your moving graphs/charts is surprisingly easy to do in MATLAB. However most of my online searches gave me old outdated methods to do it. Here’s how to make a movie or a video in MATLAB.

I kept getting this freakin’ error using the old methods (i.e. the avifile() function):
“Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.”

So I found MATLAB recommends you use the VideoWriter() class…
http://www.mathworks.com/help/techdoc/ref/videowriterclass.html

They have a nice little example in the documentation, but for the impatient, here’s my quick and dirty implementation of it (with some modifications/additions of course).
Continue reading “MATLAB – how to make a movie of plots”

MATLAB – How to scale/normalize values in a matrix to be between 0 and 1

I hate that I have to keep looking this up…

Here’s how to scale or normalize your numbers in MATLAB so they lie between 0 and 1.
Change the number of mins and maxs depending on the dimensionality of your matrix.

I = [ 1 2 3; 4 5 6]; % Some n x m matrix I that contains unscaled values.
scaledI = (I-min(I(:))) ./ (max(I(:)-min(I(:))));
min(scaledI(:)) % the min is 0
max(scaledI(:)) % the max 1

All of scaledI values are now between 0 and 1.

MATLAB – How to check if a file or a folder exists

So you want to check if a file or a folder exists in MATLAB? Here’s how to do it.

% The file/folder to find.
fPath = 'aLittleFile.m';
 
% To see what each of these "magic numbers" mean, go to, 
% http://www.mathworks.com/help/techdoc/ref/exist.html
if isequal(exist(fPath,'file'),2) % 2 means it's a file.
    % We have a file!
    display('a file!');
elseif isequal(exist(fPath, 'dir'),7) % 7 = directory.
    % We have a folder!
    display('a folder');
else
    % We have an invalid file or folder.
    display('an error!');
end

Note that we use the isequal bit to check if it is actually a file or a folder. If we take the isequal check out, then the first if statement would be true for a file and a folder (since exist(fPath,’file’) would return 7, thus the if statement would be true)!

Run a MATLAB function/script with parameters/arguments from the command line

Here’s how to run a MATLAB function with parameters from the command line.

> matlab -r "littleFunction batman superman"

where littleFunction is the name of your MATLAB file (i.e. littleFunction.m) and batman is the first parameter and superman is the second parameter. Note the quotes around the function name and the parameters! Note that the function name does NOT include the “*.m” extension.

If you need a bit more of an example, read on…

First we create a little function with two parameters.

%%%%%%
% Create a function.
function littleFunction(parameterA, parameterB)
 
display(parameterA);
display(parameterB);
 
% Uncomment this to exit MATLAB when complete.
%exit;

Note that little exit; at the end can be used if you want to close MATLAB immediately when it reaches the end of your code.

Then we navigate to the directory where littleFunction lives, and from the command line we type,

> matlab -r "littleFunction batman superman"

When we run command, MATLAB will start and run this function. You will see batman and superman being displayed.

parameterA =
 
batman
 
parameterB =
 
superman

And you’re done!