NumPy For MATLAB Users - Mathesaurus
NumPy For MATLAB Users - Mathesaurus
Help
Using interactively
Operators
MATLAB/Octave Python Description
help - Help on operator syntax
Arithmetic operators
Relational operators
MATLAB/Octave Python Description
a == b a == b or equal(a,b) Equal
a < b a < b or less(a,b) Less than
a > b a > b or greater(a,b) Greater than
a <= b a <= b or less_equal(a,b) Less than or equal
a >= b a >= b or greater_equal(a,b) Greater than or equal
a ~= b a != b or not_equal(a,b) Not Equal
Logical operators
Round off
Mathematical constants
MATLAB/Octave Python Description
pi math.pi $\pi=3.141592$
exp(1) math.e or math.exp(1) $e=2.718281$
Complex numbers
MATLAB/Octave Python Description
i z = 1j Imaginary unit
z = 3+4i z = 3+4j or z = complex(3,4) A complex number, $3+4i$
abs(z) abs(3+4j) Absolute value (modulus)
real(z) z.real Real part
imag(z) z.imag Imaginary part
arg(z) Argument
conj(z) z.conj(); z.conjugate() Complex conjugate
Trigonometry
Vectors
MATLAB/Octave Python Description
a=[2 3 4 5]; a=array([2,3,4,5]) Row vector, $1 \times n$-matrix
adash=[2 3 4 5]'; array([2,3,4,5])[:,NewAxis] Column vector, $m \times 1$-matrix
array([2,3,4,5]).reshape(-1,1)
r_[1:10,'c']
Sequences
MATLAB/Octave Python Description
1:10 arange(1,11, dtype=Float) 1,2,3, ... ,10
range(1,11)
0:9 arange(10.) 0.0,1.0,2.0, ... ,9.0
1:3:10 arange(1,11,3) 1,4,7,10
10:-1:1 arange(10,0,-1) 10,9,8, ... ,1
10:-3:1 arange(10,0,-3) 10,7,4,1
linspace(1,10,7) linspace(1,10,7) Linearly spaced vector of n=7 points
reverse(a) a[::-1] or Reverse
a(:) = 3 a.fill(3), a[:] = 3 Set all values to same scalar value
Concatenation (vectors)
Repeating
MATLAB/Octave Python Description
[a a] concatenate((a,a)) 1 2 3, 1 2 3
a.repeat(3) or 1 1 1, 2 2 2, 3 3 3
a.repeat(a) or 1, 2 2, 3 3 3
Matrices
Array creation
Assignment
MATLAB/Octave Python Description
a(:,1) = 99 a[:,0] = 99
a(:,1) = [99 98 97]' a[:,0] = array([99,98,97])
a(a>90) = 90; (a>90).choose(a,90) Clipping: Replace all elements over
a.clip(min=None, max=90) 90
a.clip(min=2, max=5) Clip upper and lower values
Sorting
Matrix manipulation
Equivalents to "size"
MATLAB/Octave Python Description
size(a) a.shape or a.getshape() Matrix dimensions
size(a,2) or length(a) a.shape[1] or size(a, axis=1) Number of columns
length(a(:)) a.size or size(a[, axis=None]) Number of elements
ndims(a) a.ndim Number of dimensions
a.nbytes Number of bytes used in memory
Multi-way arrays
MATLAB/Octave Python Description
a = cat(3, [1 2; 1 2],[3 4; 3 4]); a = array([[[1,2],[1,2]], [[3,4], Define a 3-way array
[3,4]]])
a(1,:,:) a[0,...]
Plotting
Log plots
Functions
MATLAB/Octave Python Description
f = inline('sin(x/3) - cos(x/5)') Defining functions
ezplot(f,[0,40]) x = arrayrange(0,40,.5) Plot a function for given range
fplot('sin(x/3) - cos(x/5)',[0,40]) y = sin(x/3) - cos(x/5)
% no ezplot plot(x,y, 'o')
Polar plots
Histogram plots
MATLAB/Octave Python Description
hist(randn(1000,1))
hist(randn(1000,1), -4:4)
plot(sort(a))
3d data
Data analysis
Statistics
MATLAB/Octave Python Description
mean(a) a.mean(axis=0) Average
mean(a [,axis=0])
median(a) median(a) or median(a [,axis=0]) Median
std(a) a.std(axis=0) or std(a [,axis=0]) Standard deviation
var(a) a.var(axis=0) or var(a) Variance
corr(x,y) correlate(x,y) or corrcoef(x,y) Correlation coefficient
cov(x,y) cov(x,y) Covariance
Non-linear methods
Differential equations
Fourier analysis
MATLAB/Octave Python Description
fft(a) fft(a) or Fast fourier transform
ifft(a) ifft(a) or Inverse fourier transform
convolve(x,y) Linear convolution
Loops
Conditionals
Debugging