NumPy for MATLAB users – Mathesaurus

NumPy for MATLAB users

Help

MATLAB/Octave

Python

Description

doc

help -i % browse with Info

help()

Browse help interactively

help

Help on using help

Help for a function

Help for a toolbox/library package

Demonstration examples

help help

or doc

doc

help plot

help splines

help(plot)

or doc

splines

or ?plot

help(pylab)

demo

Searching available documentation

MATLAB/Octave

Python

Description

help

help(); modules [Numeric]

which plot

help(plot)

Search help files

List available packages

Locate functions

lookfor plot

Using interactively

MATLAB/Octave

Python

Description

octave -q

ipython -pylab

Start session

Auto completion

Run code from file

Command history

Save command history

End session

TAB

or M-?

TAB

foo(.m)

execfile('foo.py')

history

hist -n

or run

diary on [..] diary off

exit

or quit

CTRL-D

foo.py

CTRL-Z # windows

sys.exit()

Operators

MATLAB/Octave

Python

Description

Help on operator syntax

help -

Arithmetic operators

MATLAB/Octave

Python

Description

a=1; b=2;

a=1; b=1

a + b

a + b

a - b

a -

a * b

a *

a / b

a /

Assignment; defining a number

Addition

Subtraction

Multiplication

Division

or add(a,b)

b or subtract(a,b)

b or multiply(a,b)

b or divide(a,b)

a .^ b

Power, $a^b$

a ** b

power(a,b)

pow(a,b)

rem(a,b)

Remainder

a % b

remainder(a,b)

fmod(a,b)

a+=1

a+=b

or add(a,b,a)

In place operation to save array

creation overhead

Factorial, $n!$

factorial(a)

Relational operators

MATLAB/Octave

Python

a == b

a == b

a < b

a

a > b

a

a = b

a

a ~= b

a

Description

or equal(a,b)

< b or less(a,b)

> b or greater(a,b)

= b or greater_equal(a,b)

!= b or not_equal(a,b)

Equal

Less than

Greater than

Less than or equal

Greater than or equal

Not Equal

Logical operators

MATLAB/Octave

Python

Description

a && b

a and b

a || b

a or b

Short-circuit logical AND

Short-circuit logical OR

Element-wise logical AND

Element-wise logical OR

Logical EXCLUSIVE OR

Logical NOT

or and(a,b)

b or or(a,b)

a & b

a |

xor(a, b)

~a

~a

or not(a)

or !a

or a and

logical_or(a,b) or a or b

logical_and(a,b)

b

logical_xor(a,b)

logical_not(a)

or not

a

True if any element is nonzero

True if all elements are nonzero

any(a)

all(a)

root and logarithm

MATLAB/Octave

Python

Description

sqrt(a)

math.sqrt(a)

log(a)

math.log(a)

log10(a)

math.log10(a)

log2(a)

math.log(a, 2)

exp(a)

math.exp(a)

Square root

Logarithm, base $e$ (natural)

Logarithm, base 10

Logarithm, base 2 (binary)

Exponential function

MATLAB/Octave

Python

Description

round(a)

around(a)

ceil(a)

ceil(a)

Round off

or math.round(a)

Round

Round up

floor(a)

floor(a)

fix(a)

fix(a)

Round down

Round towards zero

Mathematical constants

MATLAB/Octave

Python

Description

pi

math.pi

exp(1)

math.e

$\pi=3.141592$

$e=2.718281$

or math.exp(1)

Missing values; IEEE-754 floating point status flags

MATLAB/Octave

Python

Description

NaN

nan

Inf

inf

minus_zero

Not a Number

Infinity, $\infty$

Infinity, $+\infty$

Infinity, $-\infty$

Plus zero, $+0$

Minus zero, $-0$

MATLAB/Octave

Python

Description

i

z = 1j

z = 3+4i

z = 3+4j

abs(z)

abs(3+4j)

real(z)

z.real

imag(z)

z.imag

Imaginary unit

A complex number, $3+4i$

Absolute value (modulus)

Real part

Imaginary part

Argument

Complex conjugate

plus_inf

minus_inf

plus_zero

Complex numbers

or z

= complex(3,4)

arg(z)

conj(z)

z.conj(); z.conjugate()

Trigonometry

MATLAB/Octave

Python

Description

atan(a,b)

atan2(b,a)

Arctangent, $\arctan(b/a)$

Hypotenus; Euclidean distance

hypot(x,y)

Generate random numbers

MATLAB/Octave

Python

Description

rand(1,10)

random.random((10,))

Uniform distribution

random.uniform((10,))

2+5*rand(1,10)

random.uniform(2,7,(10,))

rand(6)

random.uniform(0,1,(6,6))

randn(1,10)

random.standard_normal((10,))

Uniform: Numbers between 2

and 7

Uniform: 6,6 array

Normal distribution

Vectors

MATLAB/Octave

Python

Description

a=[2 3 4 5];

a=array([2,3,4,5])

adash=[2 3 4 5]';

array([2,3,4,5])[:,NewAxis]

Row vector, $1 \times n$-matrix

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.)

1:3:10

arange(1,11,3)

10:-1:1

arange(10,0,-1)

10:-3:1

arange(10,0,-3)

linspace(1,10,7)

linspace(1,10,7)

reverse(a)

a[::-1]

a(:) = 3

a.fill(3), a[:] = 3

or

0.0,1.0,2.0, ... ,9.0

1,4,7,10

10,9,8, ... ,1

10,7,4,1

Linearly spaced vector of n=7

points

Reverse

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

1 1 1, 2 2 2, 3 3 3

1, 2 2, 3 3 3

or

a.repeat(a) or

a.repeat(3)

Miss those elements out

MATLAB/Octave

Python

Description

a(2:end)

a[1:]

miss the first element

miss the tenth element

last element

last two elements

a([1:9])

a(end)

a[-1]

a(end-1:end)

a[-2:]

Maximum and minimum

MATLAB/Octave

Python

Description

max(a,b)

maximum(a,b)

max([a b])

concatenate((a,b)).max()

[v,i] = max(a)

v,i = a.max(0),a.argmax(0)

pairwise max

max of all values in two vectors

Vector multiplication

MATLAB/Octave

Python

Description

a.*a

a*a

dot(u,v)

dot(u,v)

Multiply two vectors

Vector dot product, $u \cdot v$

MATLAB/Octave

Python

Description

a = [2 3;4 5]

a = array([[2,3],[4,5]])

Define a matrix

Matrices

Concatenation (matrices); rbind and cbind

MATLAB/Octave

Python

Description

[a ; b]

concatenate((a,b), axis=0)

Bind rows

vstack((a,b))

[a , b]

concatenate((a,b), axis=1)

hstack((a,b))

Bind columns

Bind slices (three-way

dstack((a,b))

arrays)

concatenate((a,b), axis=None)

Concatenate matrices into

one vector

concatenate((r_[1:5],r_[1:5])).reshape(2,-1) Bind rows (from vectors)

concatenate((a,b), axis=2)

[a(:), b(:)]

[1:4 ; 1:4]

vstack((r_[1:5],r_[1:5]))

Bind columns (from

vectors)

[1:4 ; 1:4]'

Array creation

MATLAB/Octave

Python

Description

zeros(3,5)

zeros((3,5),Float)

0 filled array

0 filled array of integers

1 filled array

Any number filled array

Identity matrix

Diagonal

Magic squares; Lo Shu

Empty array

zeros((3,5))

ones(3,5)

ones((3,5),Float)

ones(3,5)*9

eye(3)

identity(3)

diag([4 5 6])

diag((4,5,6))

magic(3)

a = empty((3,3))

Reshape and flatten matrices

MATLAB/Octave

Python

Description

reshape(1:6,3,2)';

arange(1,7).reshape(2,-1)

Reshaping (rows first)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download