Matlab To Numpy PDF
Matlab To Numpy PDF
html
Using interactively
MATLAB/Octave Python Description
octave -q ipython -pylab Start session
TAB or M-? TAB Auto completion
foo(.m) execfile('foo.py') or run foo.py Run code from file
history hist -n Command history
diary on [..] diary off Save command history
exit or quit CTRL-D End session
CTRL-Z # windows
sys.exit()
Operators
MATLAB/Octave Python Description
help - Help on operator syntax
Arithmetic operators
MATLAB/Octave Python Description
a=1; b=2; a=1; b=1 Assignment; defining a number
a + b a + b or add(a,b) Addition
a - b a - b or subtract(a,b) Subtraction
1 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
a * b a * b or multiply(a,b) Multiplication
a / b a / b or divide(a,b) Division
a .^ b a ** b Power, $a^b$
power(a,b)
pow(a,b)
rem(a,b) a % b Remainder
remainder(a,b)
fmod(a,b)
a+=1 a+=b or add(a,b,a) In place operation to save array
creation overhead
factorial(a) Factorial, $n!$
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
MATLAB/Octave Python Description
a && b a and b Short-circuit logical AND
a || b a or b Short-circuit logical OR
a & b or and(a,b) logical_and(a,b) or a and b Element-wise logical AND
a | b or or(a,b) logical_or(a,b) or a or b Element-wise logical OR
xor(a, b) logical_xor(a,b) Logical EXCLUSIVE OR
~a or not(a) logical_not(a) or not a Logical NOT
~a or !a
any(a) True if any element is nonzero
all(a) True if all elements are nonzero
Round off
2 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
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
MATLAB/Octave Python Description
atan(a,b) atan2(b,a) Arctangent, $\arctan(b/a)$
hypot(x,y) Hypotenus; Euclidean distance
3 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Vectors
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)
MATLAB/Octave Python Description
[a a] concatenate((a,a)) Concatenate two vectors
[1:4 a] concatenate((range(1,5),a),
axis=1)
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
4 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Vector multiplication
Matrices
Array creation
5 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
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
6 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Sum
MATLAB/Octave Python Description
sum(a) a.sum(axis=0) Sum of each column
sum(a') a.sum(axis=1) Sum of each row
sum(sum(a)) a.sum() Sum of all elements
a.trace(offset=0) Sum along diagonal
cumsum(a) a.cumsum(axis=0) Cumulative sum (columns)
Sorting
7 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Matrix manipulation
Equivalents to "size"
8 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Multi-way arrays
MATLAB/Octave Python Description
a = cat(3, [1 2; 1 2],[3 4; 3 a = array([[[1,2],[1,2]], [[3,4], Define a 3-way array
4]); [3,4]]])
a(1,:,:) a[0,...]
Plotting
9 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Log plots
MATLAB/Octave Python Description
semilogy(a) semilogy(a) logarithmic y-axis
semilogx(a) semilogx(a) logarithmic x-axis
loglog(a) loglog(a) logarithmic x and y axes
Functions
MATLAB/Octave Python Description
f = inline('sin(x/3) - Defining functions
cos(x/5)')
ezplot(f,[0,40]) x = arrayrange(0,40,.5) Plot a function for given range
fplot('sin(x/3) - cos(x/5)', y = sin(x/3) - cos(x/5)
[0,40]) plot(x,y, 'o')
% no ezplot
Polar plots
MATLAB/Octave Python Description
theta = 0:.001:2*pi; theta = arange(0,2*pi,0.001)
r = sin(2*theta); r = sin(2*theta)
polar(theta, rho) polar(theta, rho)
Histogram plots
10 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
3d data
11 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
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
12 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Non-linear methods
Differential equations
MATLAB/Octave Python Description
diff(a) diff(x, n=1, axis=0) Discrete difference function and
approximate derivative
Solve 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
Programming
MATLAB/Octave Python Description
.m .py Script file extension
% # Comment symbol (rest of line)
% or #
% must be in MATLABPATH from pylab import * Import library functions
% must be in LOADPATH
string='a=234'; string="a=234" Eval
eval(string) eval(string)
Loops
13 of 14 05/03/2019, 11:03
NumPy for MATLAB users – Mathesaurus http://mathesaurus.sourceforge.net/matlab-numpy.html
Conditionals
MATLAB/Octave Python Description
if 1>0 a=100; end if 1>0: a=100 if-statement
if 1>0 a=100; else a=0; end if-else-statement
Debugging
MATLAB/Octave Python Description
ans Most recent evaluated expression
whos or who List variables loaded into memory
clear x or clear [all] Clear variable $x$ from memory
disp(a) print a Print
14 of 14 05/03/2019, 11:03