6 2 NumPy For MATLAB Users
6 2 NumPy For MATLAB Users
Help
Using interactively
Operators
MATLAB/Octave Python Description
help - Help on operator syntax
Arithmetic operators
Relational operators
MATLAB/Octave Python Description
a == b a == bor equal(a,b) Equal
a<b a < bor less(a,b) Less than
a>b a > bor greater(a,b) Greater than
a <= b a <= bor less_equal(a,b) Less than or equal
a >= b a >= bor greater_equal(a,b) Greater than or equal
a ~= b a != bor not_equal(a,b) Not Equal
Logical operators
Mathematical constants
Complex numbers
Trigonometry
MATLAB/Octave Python Description
atan(a,b) atan2(b,a) Arctangent, $\arctan(b/a)$
hypot(x,y) Hypotenus; Euclidean distance
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$-
array([2,3,4,5]).reshape(-1,1) matrix
r_[1:10,'c']
Sequences
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
Vector multiplication
Matrices
MATLAB/Octave Python Description
a = [2 3;4 5] a = array([[2,3],[4,5]]) Define a matrix
Array creation
Assignment
Sum
Sorting
MATLAB/Octave Python Description
a=[432;286;147] a = array([[4,3,2],[2,8,6], Example data
[1,4,7]])
sort(a(:)) a.ravel().sort()or Flat and sorted
sort(a) a.sort(axis=0)or msort(a) Sort each column
sort(a')' a.sort(axis=1) Sort each row
sortrows(a,1) a[a[:,0].argsort(),] Sort rows (by first row)
a.ravel().argsort() Sort, return indices
a.argsort(axis=0) Sort each column, return indices
a.argsort(axis=1) Sort each row, return indices
Maximum and minimum
Matrix manipulation
Equivalents to "size"
Multi-way arrays
Plotting
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
MATLAB/Octave Python Description
hist(randn(1000,1))
hist(randn(1000,1), -4:4)
plot(sort(a))
3d data
Data analysis
Statistics
Non-linear methods
Differential equations
Fourier analysis
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
MATLAB/Octave Python Description
for i=1:5; disp(i); end for i in range(1,6): print(i) for-statement
for i=1:5 for i in range(1,6): Multiline for statements
disp(i) print(i)
disp(i*2) print(i*2)
end
Conditionals
Debugging
MATLAB/Octave Python Description
ans Most recent evaluated expression
whosor who List variables loaded into memory
clear xor clear [all] Clear variable $x$ from memory
disp(a) print a Print