Chris R. Smith

Here are some one-to-one translations for many popular functions in Matlab and python that should make it easier to switch from Matlab to Python

## System functions¶

Matlab Function

Python Equivalent

Description

sys.path.append

## File I/O¶

Matlab Function

Python Equivalent

Description

Read ASCII-delimited file of numeric data into matrix

read image file; N is number of files used

## Data Type¶

Matlab Function

Python Equivalent

Description

int

numpy.int

Convert data to signed integer

double

numpy.float

Convert data to double

real

numpy.real

Return the real part of a complex number

imag

numpy.imag

Return the imaginary part of a complex number

## Mathematics¶

Matlab Function

Python Equivalent

Description

sqrt

math.sqrt or numpy.sqrt

Square root

erf

math.erf or scipy.special.erf

Error function

atan2

math.erf or numpy.atan2

abs

abs or numpy.abs

Absolute value

exp

exp or numpy.exp

Exponential function

sin

sin or numpy.sin

Sine function

## Array Creation¶

Matlab Function

Python Equivalent

Description

zeros

numpy.zeros

Create an array of zeros

meshgrid

numpy.meshgrid

Create grid of coordinates in 2 or 3 dimensions

ndgrid

numpy.mgrid or numpy.ogrid

Rectangular grid in N-D space

Matlab Function

Python Equivalent

Description

permute

numpy.transpose

Rearrange dimensions of N-dimensional array

angle

numpy.angle

Phase angles for elements in complex array

max

numpy.max

Return the maximum element in an array

min

numpy.min

Return the minimum element in an array

reshape

numpy.reshape

Reshape array

mean

numpy.mean

Take mean along specified dimension

size

numpy.size

get the total number of entries in an array

cell2mat

numpy.vstack([numpy.hstack(cell) for cell in cells])

converts data structure from cell to mat; joins multiple arrays of different sizes into single array

repmat

numpy.tile

Repeat copies of an array

unwrap

np.unwrap

Shift the phase of an array so that there are no jumps of more than the desired angle (default pi)

## Array Indexing¶

Matlab Function

Python Equivalent

Description

find

numpy.where

Find all indices of a matrix for which a logical statement is true

isnan

numpy.isnan

checks each array entry to see if it is NaN

isinf

numpy.isinf

checks each array entry to see if it is Inf

ischar

numpy.ischar

checks each array entry to see if it is a character

Matlab Function

Python Equivalent

Description

fft2

numpy.fft.fft2

2D fast Fourier transform

fftshift

numpy.fft.fftshift

shift zero-frequency component to the center of the spectrum

ifftshift

numpy.fft.ifftshift

inverse fftshift

ifft2

numpy.fft.fifft2

inverse 2d fft

interp2

scipy.interpolate.RectBivariateSpline or scipy.interpolate.interp2

Interpolation for 2-D gridded data in meshgrid format

imshowpair

skimage.measure.structural_similarity

Compare differences between 2 images

imregconfig

Creates configurations to perform intensity-based image registration

imregister

Intensity-based image registration

imregtform

skimage.feature.register_translation or skimage.transform.estimate_transform

Estimate geometric transfomation to align two images

imwarp

skimage.transform.warp

Apply geometric transformation to an image

imref2d

Reference 2d image to xy-coordinates

corr2

scipy.signal.correlate2d

2d correlation coefficient

optimset

Create of edit optimizations options for passing to fminbnd, fminsearch, fzero, or lsqnonneg

lsqcurvefit

scipy.optimize.curve_fit

Solve nonlinear curve-fitting problems

fastica

sklearn.decomposition.FastICA

fast fixed-point algorithm for independent component analysis and projection pursuit

kmeans

sklearn.cluster.Kmeans

kmeans clustering

fsolve

scipy.optimize.root(func, x0, method=’anderson’)

Root finding. Scipy does not have a trust-region dogleg method that functions exactly like Matlab’s fsolve. The ‘anderson’ method reproduces the results in many cases. Other methods may need to be explored for other problems.

## Basic Plotting¶

Matlab Function

Python Equivalent

Description

figure

matplotlib.pyplot.figure

Create a new figure object

clf

figure.clf

clear figure; shouldn’t be needed in Python since each figure will be a unique object

subplot

1stcreates a set of subplots in the figure, 2ndcreates one subplot and adds it to the figure

plot

figure.plot or axes.plot

title

object.title

Title of plot; better to define on object creation if possible

xlabel

axes.xlabel

Label for the x-axis of plot

ylabel

axes.ylabel

Label for the y-axis of plot

imagesc

pyplot.imshow or pyplot.matshow

Scale image data to full range of colormap and display

axis

axes.axis

Axis properties

surf

axes3d.plot_surface or axes3d.plot_trisurf

Plot a 3d surface, need to uses mpl_toolkits.mplot3d and Axes3d; which you use depends on data format

Set during plot creation as argument

view

axes3d.view_init

Change the viewing angle for a 3d plot

colormap

plot.colormap

Set the colormap; better to do so at plot creation if possible

colorbar