NumPy for MATLAB users - GitHub Pages

NumPy for MATLAB users

Help

MATLAB/Octave

Python

Description

doc

help -i % browse with Info

help()

Browse help interactively

help helpor doc doc

help

help plot

help(plot)or ?plot

help splinesor doc splines

help(pylab)

Help on using help

Help for a function

Help for a toolbox/library package

Demonstration examples

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

TABor M-?

TAB

foo(.m)

execfile('foo.py')or run foo.py

history

hist -n

Start session

Auto completion

Run code from file

Command history

Save command history

End session

diary on [..] diary off

exitor quit

CTRL-D

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 + bor add(a,b)

Assignment; defining a number

Addition

a-b

a - bor subtract(a,b)

a*b

a * bor multiply(a,b)

a/b

a / bor divide(a,b)

a .^ b

a ** b

Subtraction

Multiplication

Division

Power, $a^b$

power(a,b)

pow(a,b)

rem(a,b)

a%b

remainder(a,b)

fmod(a,b)

Remainder

a+=1

a+=bor add(a,b,a)

In place operation to save array

creation overhead

Factorial, $n!$

factorial(a)

Relational operators

MATLAB/Octave

Python

Description

a == b

a == bor equal(a,b)

ab

a > bor greater(a,b)

a = bor greater_equal(a,b)

a ~= b

a != bor 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

a & bor and(a,b)

logical_and(a,b)or a and b

a | bor or(a,b)

logical_or(a,b)or a or b

xor(a, b)

logical_xor(a,b)

~aor not(a)

~aor !a

logical_not(a)or not a

Short-circuit logical AND

Short-circuit logical OR

Element-wise logical AND

Element-wise logical OR

Logical EXCLUSIVE OR

Logical NOT

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

Round off

MATLAB/Octave

Python

Description

round(a)

around(a)or math.round(a)

ceil(a)

ceil(a)

floor(a)

floor(a)

fix(a)

fix(a)

Round

Round up

Round down

Round towards zero

Mathematical constants

MATLAB/Octave

Python

Description

pi

math.pi

exp(1)

math.eor math.exp(1)

$\pi=3.141592$

$e=2.718281$

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+4jor z = complex(3,4)

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

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

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

Vectors

array([2,3,4,5]).reshape(-1,1)

r_[1:10,'c']

Sequences

MATLAB/Octave

Python

Description

1:10

arange(1,11, dtype=Float)

range(1,11)

1,2,3, ... ,10

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]or

a(:) = 3

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

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

a.repeat(3)or

a.repeat(a)or

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

pairwise max

max of all values in two vectors

[v,i] = max(a)

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

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)

vstack((a,b))

Bind rows

[a , b]

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

Bind columns

hstack((a,b))

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

zeros((3,5))

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

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

Google Online Preview   Download