-
Notifications
You must be signed in to change notification settings - Fork 46
PDL for Matlab users
If you are a MATLAB user, this page is for you. It explains the key differences between MATLAB and PDL to help you get going as quickly as possible.
This document is not a tutorial. For that, go to the PDL Quick Start guide. This document complements the Quick Start guide, as it highlights the differences between MATLAB and PDL.
The key difference between MATLAB and PDL is Perl.
Perl is a general purpose programming language with thousands of modules freely available on the web. PDL is an extension of Perl. This gives PDL programs access to more features than most numerical tools can dream of.
Perl itself has excellent documentation at http://perldoc.perl.org. Or use the command perldoc perl.
To search through Perl modules, go to http://www.cpan.org
Like MATLAB, PDL is an array-based numerical programming language. This section covers the basic array syntax.
The shell
Run the **perldl** command to start the PDL interactive shell.
Variables
In Perl, variables always start with the '$' sign.
value = 42 MATLAB
$value = 42 PDL
Array
Use the "pdl" constructor to create a new PDL object.
v = [1,2,3,4] MATLAB
$v = pdl [1,2,3,4] PDL
Matrix
A = [ 1,2,3 ; 3,4,5 ] MATLAB
$A = pdl [ [1,2,3] , [3,4,5] ] PDL
Display a matrix
disp(A) MATLAB
print $A PDL
p $A PDL
Matrix multiplication
A * B MATLAB
$A x $B PDL
Component-wise multiplication
A .* B MATLAB
$A * $B PDL
Transpose
A' MATLAB
transpose $A PDL
Create a matrix of zeros
A = zeros(5) MATLAB
$A = zeros 5,5 PDL
Create a matrix of ones
A = ones(5) MATLAB
$A = ones 5,5 PDL
Create a random matrix
A = rand(5) MATLAB
$A = random 5,5 PDL
Average of a matrix along one dimension
mean(A) MATLAB
average $A PDL
Average of all elements in a matrix
mean(A(:)) MATLAB
avg $A PDL
Sum of a matrix along one dimension
sum(A) MATLAB
sumover $A PDL
Sum of all elements in a matrix
sum(A(:)) MATLAB
sum $A PDL
Maximum of a matrix along one dimension
max(A) MATLAB
maximum $A PDL
Maximum of all elements in a matrix
max(A(:)) MATLAB
max $A PDL
Minimum of a matrix along one dimension
min(A) MATLAB
minimum $A PDL
Minimum of all elements in a matrix
min(A(:)) MATLAB
min $A PDL
Generate linearly spaced vector
linspace(a,b,n) MATLAB
zeroes(n)->xlinvals(a,b) PDL
Trig functions: sin, cos, tan, asin, acos, atan
Same in both languages.
Exponential and Log: exp, log, log10
Same in both languages.
Dimension sizes
size(A) MATLAB
shape $A PDL (see also dims $A which returns a perl list/array)
Number of dimensions
ndims(A) MATLAB
ndims $A PDL
Number of elements
numel(A) MATLAB
nelem $A PDL
A standard conditional ("if-statement") in Perl behaves like the one in MATLAB, with a more C-like syntax:
% MATLAB conditional
if value > MAX
disp('Value too large')
elseif value < MIN
disp('Value too small')
else
disp('Value is perfect\n')
end
# Perl conditional
if ($value > $MAX) {
print "Value too large\n";
} elsif ($value < $MIN) {
print "Value too small\n";
} else {
print "Value is perfect\n";
}
But unlike MATLAB, Perl has other conditionals that can make the code read more natural. For example, the "unless" statement behaves as the complement to "if":
if ( ! error() ) {
... keep working ...
}
unless ( error() ) {
... keep working ...
}
The second block reads more natural ("Unless there is an error, keep working"). Another way to make code more readable is to use Perl's post-fix notation for "if" and "unless":
print "Warning: Value too large" if ( $value > $MAX );
keep_working() unless ( invalid($value) );
Again, this can read very natural ("Keep working unless there is a problem"). Postfix notation is often used to highlight an error condition (as in the first example) or to express (in the code) that you expect a particular statement to get executed most of the time (as in the second example).
There is another conditional which will be familiar to C programmers:
$result = ( valid($value) ? "ok" : "error" );
This is equivalent to the more verbose:
if ( valid($value) ) {
$result = "ok";
} else {
$result = "error";
}
Names of the functions can be looked up in the list of Matlab's functions. You might also consider checking out Matlab's online documentation.
Feature | Matlab | Perl/PDL |
---|---|---|
Command to terminate interactive session | exit | exit, quit, x, q |
Script file to run when closing | finish | Not available (?) |
Startup config file | matlabrc | .perldlrc |
Feature | Matlab | Perl/PDL |
---|---|---|
Feature | Matlab | Perl/PDL |
---|---|---|
Close a file | fclose | close |
Feature | Matlab | Perl/PDL |
---|---|---|
Break a filename into path-and-name components | fileparts | File::Basename |
Check that a file exists | ??? error-free open? | -e operator |
Search a directory tree | ??? | File::Find |
Feature | Matlab | Perl/PDL |
---|---|---|
create a matrix of zeros | zeros | zeros, zeroes |
create a matrix of [0,1] random numbers | rand | random |
transpose | transpose, ' | transpose |
find average | mean | avg, average - for first dimension |
find maximum | max | max, maximum - for first dimension |
find minimum | min | min, minimum - for first dimension |
inner product | a * b' | inner |
outer product | a' * b | outer |
sum of elements | sum - along columns | sumover - along 1st dimension (rows) |
generate linearly spaced vector | linspace(a,b,n) | zeroes(n)->xlinvals(a,b); |
Feature | Matlab | Perl/PDL |
---|---|---|
dimension sizes | size | shape or dims |
number of dimensions | ndims | ndims |
number of elements | numel | nelem |
- abs - Core perl abs works on PDLs as well
- cat - see pdl(), cat(), and append() for PDL routines
- double - in PDL::Core
- end - in Perl complex expressions are wrapped by curly braces { }, no end keyword is used
- exit - Core perl exit, the finish.m probably maps to the END blocks in Perl
- function - Core perl sub,
- if - See the if statement in perldoc
- length - no easy "get the size of the longest dimension" here, calculate as pdl($pdl->dims)->max, see dims in PDL::Core
- magic - see http://search.cpan.org/dist/Math-MagicSquare-Generator or http://search.cpan.org/dist/Math-MagicSquare
- max - see List::Util for lists
- repmat - see PDL dummy() and clump() methods
- size(A) - see PDL shape() method, also dims() which returns perl list and not PDL vector of values
- size(A, N) - see PDL dim() method
- um2str
- unique - see PDL uniq() method for values and uniqvec() for unique(A,'rows')
- uniq_pairs
- unix - Core perl qx{} or backtick
- zeros - see PDL zeros()
- M-files - Modules in Perl with .pm extentsion and the package keyword at the beginning and/or *.pdl files (see PDL::AutoLoader)
- Creating a matrix: $matrix = pdl( [[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14 ,1] ] );
- sum($matrix) will return 136
- A' - transpose($matrix) from PDL::Basic
In this section we will list all the toolboxes that can be added to Matlab and see how they map to solutions using Perl and PDL.
Perl (and thus PDL) can use DBI and the database drivers in the DBD::* namespace on CPAN.