Matlab Python Xref
Matlab Python Xref
1 Help
Desc. matlab/Octave Python R
Browse help interactively doc help() help.start()
Octave: help -i % browse with Info
Help on using help help help or doc doc help help()
Help for a function help plot help(plot) or ?plot help(plot) or ?plot
Help for a toolbox/library package help splines or doc splines help(pylab) help(package=’splines’)
Demonstration examples demo demo()
Example using a function example(plot)
2 Operators
Desc. matlab/Octave Python R
Help on operator syntax help - help(Syntax)
References: Hankin, Robin. R for Octave users (), available from http://cran.r-project.org/doc/contrib/R-and-octave-.txt (accessed ..); Martelli, Alex. Python in a Nutshell (O’Reilly, );
Oliphant, Travis. Guide to NumPy (Trelgol, ); Hunter, John. The Matplotlib User’s Guide (), available from http://matplotlib.sf.net/ (accessed ..); Langtangen, Hans Petter. Python
Scripting for Computational Science (Springer, ); Ascher et al.: Numeric Python manual (), available from http://numeric.scipy.org/numpy.pdf (accessed ..); Moler, Cleve. Numerical
Computing with MATLAB (MathWorks, ), available from http://www.mathworks.com/moler/ (accessed ..); Eaton, John W. Octave Quick Reference (); Merrit, Ethan. Demo scripts for
gnuplot version 4.0 (), available from http://gnuplot.sourceforge.net/demo/ (accessed ..); Woo, Alex. Gnuplot Quick Reference (), available from http://www.gnuplot.info/docs/gpcard.pdf
(accessed ..); Venables & Smith: An Introduction to R (), available from http://cran.r-project.org/doc/manuals/R-intro.pdf (accessed ..); Short, Tom. R reference card (), available
from http://www.rpad.org/Rpad/R-refcard.pdf (accessed ..).
MATLAB commands in numerical Python (NumPy) 2
Vidar Bronken Gundersen /mathesaurus.sf.net
2.8 Trigonometry
Desc. matlab/Octave Python R
Arctangent, arctan(b/a) atan(a,b) atan2(b,a) atan2(b,a) p
Hypotenus; Euclidean distance hypot(x,y) x2 + y 2
3 Vectors
Desc. matlab/Octave Python R
Row vector, 1 × n-matrix a=[2 3 4 5]; a=array([2,3,4,5]) a <- c(2,3,4,5)
Column vector, m × 1-matrix adash=[2 3 4 5]’; array([2,3,4,5])[:,NewAxis] adash <- t(c(2,3,4,5))
array([2,3,4,5]).reshape(-1,1)
r_[1:10,’c’]
3.1 Sequences
Desc. matlab/Octave Python R
,,, ... , 1:10 arange(1,11, dtype=Float) seq(10) or 1:10
range(1,11)
.,.,., ... ,. 0:9 arange(10.) seq(0,length=10)
,,, 1:3:10 arange(1,11,3) seq(1,10,by=3)
,,, ... , 10:-1:1 arange(10,0,-1) seq(10,1) or 10:1
,,, 10:-3:1 arange(10,0,-3) seq(from=10,to=1,by=-3)
Linearly spaced vector of n= points linspace(1,10,7) linspace(1,10,7) seq(1,10,length=7)
Reverse reverse(a) a[::-1] or rev(a)
Set all values to same scalar value a(:) = 3 a.fill(3), a[:] = 3
3.3 Repeating
Desc. matlab/Octave Python R
, [a a] concatenate((a,a)) rep(a,times=2)
, , a.repeat(3) or rep(a,each=3)
, , a.repeat(a) or rep(a,a)
4 Matrices
Desc. matlab/Octave Python R h i
2 3
Define a matrix a = [2 3;4 5] a = array([[2,3],[4,5]]) rbind(c(2,3),c(4,5))
4 5
array(c(2,3,4,5), dim=c(2,2))
4.6 Assignment
Desc. matlab/Octave Python R
a(:,1) = 99 a[:,0] = 99 a[,1] <- 99
a(:,1) = [99 98 97]’ a[:,0] = array([99,98,97]) a[,1] <- c(99,98,97)
Clipping: Replace all elements over a(a>90) = 90; (a>90).choose(a,90) a[a>90] <- 90
a.clip(min=None, max=90)
4.8 Sum
Desc. matlab/Octave Python R
Sum of each column sum(a) a.sum(axis=0) apply(a,2,sum)
Sum of each row sum(a’) a.sum(axis=1) apply(a,1,sum)
Sum of all elements sum(sum(a)) a.sum() sum(a)
Sum along diagonal a.trace(offset=0)
Cumulative sum (columns) cumsum(a) a.cumsum(axis=0) apply(a,2,cumsum)
MATLAB commands in numerical Python (NumPy) 8
Vidar Bronken Gundersen /mathesaurus.sf.net
4.9 Sorting
Desc. matlab/Octave Python R
4 3 2
Example data a = [ 4 3 2 ; 2 8 6 ; 1 4 7 ] a = array([[4,3,2],[2,8,6],[1,4,7]]) 2 8 6
1 4 7
1 2 2
Flat and sorted sort(a(:)) a.ravel().sort() or t(sort(a)) 3 4 4
6 7 8
1 3 2
Sort each column sort(a) a.sort(axis=0) or msort(a) apply(a,2,sort) 2 4 6
4 8 7
2 3 4
Sort each row sort(a’)’ a.sort(axis=1) t(apply(a,1,sort)) 2 6 8
1 4 7
1 4 7
Sort rows (by first row) sortrows(a,1) a[a[:,0].argsort(),] 2 8 6
4 3 2
Sort, return indices a.ravel().argsort() order(a)
Sort each column, return indices a.argsort(axis=0)
Sort each row, return indices a.argsort(axis=1)
Vector of non-zero values [i j v] = find(a) v = a.compress((a!=0).flat) ij <- which(a != 0, arr.ind=T); v <- a[ij]
v = extract(a!=0,a)
5 Multi-way arrays
Desc. matlab/Octave Python R
Define a -way array a = cat(3, [1 2; 1 2],[3 4; 3 4]); a = array([[[1,2],[1,2]], [[3,4],[3,4]]])
a(1,:,:) a[0,...]
MATLAB commands in numerical Python (NumPy) 10
Vidar Bronken Gundersen /mathesaurus.sf.net
7 Plotting
7.1 Basic x-y plots
Desc. matlab/Octave Python R
4
-1
-2
-3
-4
0 20 40 60 80 100
4.0
3.5
3.0
2.5
2.0
4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
1
4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
Filled plot fill(t,s,’b’, t,c,’g’) fill(t,s,’b’, t,c,’g’, alpha=0.2) plot(t,s, type="n", xlab="", ylab="")
Octave: % fill has a bug? polygon(t,s, col="lightblue")
polygon(t,c, col="lightgreen")
5 5
6 71
7 033
Stem-and-Leaf plot stem(x[,3]) 8 00113345567889
9 0133566677788
10 32674
7.1.4 Functions
Desc. matlab/Octave Python R
x x
Defining functions f = inline(’sin(x/3) - cos(x/5)’) f <- function(x) sin(x/3) - cos(x/5) f (x) = sin 3 − cos 5
●●●
●● ●
● ●
1.0
●
● ●
● ●
●
● ●
●
● ●
●
●●●
0.5
●● ● ● ●
● ● ●
● ● ●
● ●
● ● ●
● ● ● ●
● ●
● ● ●
● ●
● ● ● ● ●
0.0
● ●
● ●● ●
●●●●
● ●
●
● ●
f (x)
●
−0.5
●
●
● ●
●
●
●
● ●
−1.0
●
●
●
●
●
●
●
−1.5
●
●
● ●
● ●
● ●
● ●
−2.0
●●●●
0 10 20 30 40
Plot a function for given range ezplot(f,[0,40]) x = arrayrange(0,40,.5) plot(f, xlim=c(0,40), type=’p’) x
135 45
180 0
225 315
270
7.4 3d data
7.4.1 Contour and image plots
Desc. matlab/Octave Python R
0.0
1
0.2
0.8
6
0.4
0.
0.6
-0.4
-0.2
-0.6
0
0.8
1.0
-1
-0.2
-2
-2 -1 0 1 2
-1
-2
-2 -1 0 1 2
0.0
1
0.2
0.8
6
0.4
0.
0.6
-0.4
-0.2
-0.6
0
0.8
1.0
-1
-0.2
-2
-2 -1 0 1 2
0.4
0.2
0.0
z
2
−0.2
1
−0.4
−2
0
y
−1
0 −1
x
1
−2
2
0.4
0.2
0.0
z
2
−0.2
1
−0.4
−2
0
y
−1
0 −1
x
1
−2
2
80
60
40
20
0
-20 100
-40
-60 90
-80 80
70
80 60
60 50
40 40
20 30
0
20
-20
-40 10
-60 0
8 Data analysis
8.1 Set membership operators
Desc. matlab/Octave Python R
Create sets a = [ 1 2 2 5 2 ]; a = array([1,2,2,5,2]) a <- c(1,2,2,5,2)
b = [ 2 3 4 ]; b = array([2,3,4]) b <- c(2,3,4)
a = set([1,2,2,5,2])
b = set([2,3,4])
Set unique unique(a) unique1d(a) unique(a) 1 2 5
unique(a)
set(a)
8.2 Statistics
Desc. matlab/Octave Python R
Average mean(a) a.mean(axis=0) apply(a,2,mean)
mean(a [,axis=0])
Median median(a) median(a) or median(a [,axis=0]) apply(a,2,median)
Standard deviation std(a) a.std(axis=0) or std(a [,axis=0]) apply(a,2,sd)
Variance var(a) a.var(axis=0) or var(a) apply(a,2,var)
Correlation coefficient corr(x,y) correlate(x,y) or corrcoef(x,y) cor(x,y)
Covariance cov(x,y) cov(x,y) cov(x,y)
10 Programming
Desc. matlab/Octave Python R
Script file extension .m .py .R
Comment symbol (rest of line) % # #
Octave: % or #
Import library functions % must be in MATLABPATH from pylab import * library(RSvgDevice)
Octave: % must be in LOADPATH
Eval string=’a=234’; string="a=234" string <- "a <- 234"
eval(string) eval(string) eval(parse(text=string))
10.1 Loops
Desc. matlab/Octave Python R
for-statement for i=1:5; disp(i); end for i in range(1,6): print(i) for(i in 1:5) print(i)
Multiline for statements for i=1:5 for i in range(1,6): for(i in 1:5) {
disp(i) print(i) print(i)
disp(i*2) print(i*2) print(i*2)
end }
10.2 Conditionals
Desc. matlab/Octave Python R
if-statement if 1>0 a=100; end if 1>0: a=100 if (1>0) a <- 100
if-else-statement if 1>0 a=100; else a=0; end
Ternary operator (if?true:false) ifelse(a>0,a,0) a > 0?a : 0
10.3 Debugging
Desc. matlab/Octave Python R
Most recent evaluated expression ans .Last.value
List variables loaded into memory whos or who objects()
Clear variable x from memory clear x or clear [all] rm(x)
Print disp(a) print a print(a)
This document is still draft quality. Most shown d plots are made using Matplotlib, and d plots using R and Gnuplot, provided as examples only.
Version numbers and download url for software used: Python .., http://www.python.org/; NumPy .., http://numeric.scipy.org/; Matplotlib ., http://matplotlib.sf.net/; IPython ..,
http://ipython.scipy.org/; R .., http://www.r-project.org/; Octave .., http://www.octave.org/; Scilab ., http://www.scilab.org/; Gnuplot ., http://www.gnuplot.info/.
For referencing: Gundersen, Vidar Bronken. MATLAB commands in numerical Python (Oslo/Norway, ), available from: http://mathesaurus.sf.net/
Contributions are appreciated: The best way to do this is to edit the xml and submit patches to our tracker or forums.