1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

PDL for Matlab users

From pdl

Jump to: navigation, search

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.

Contents

Perl

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

Language Syntax

Arrays and Matrices

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 Operations

Matrix multiplication

 A * B     MATLAB
 $A x $B   PDL

Component-wise multiplication

 A .* B    MATLAB
 $A * $B   PDL

Transpose

 A'              MATLAB
 transpose $A    PDL

Mathematical Functions

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.

Matrix dimensions

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

Conditionals

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";
 }

List of Matlab functions grouped

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.

Desktop Tools and Development Environment

Feature Matlab Perl/PDL
Startup and Shutdown
Command to terminate interactive session exit exit, quit, x, q
Script file to run when closing finish Not available (?)
Startup config file matlabrc .perldlrc
Command Window and History

Data Import and Export

Feature Matlab Perl/PDL
Low-Level File I/O
Close a file fclose close
File attributes and path handling
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

Mathematics

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);
Finding dimensions
dimension sizes size shape or dims
number of dimensions ndims ndims
number of elements numel nelem

Data Analysis

Programming and Data Types

Object-Oriented Programming

Graphics

3-D Visualization

GUI Development

External Interfaces

in ABC order

Other terms

  • 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

Toolbox

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.


Database Connectivity and Reporting

Perl (and thus PDL) can use DBI and the database drivers in the DBD::* namespace on CPAN

Personal tools