Matlab and R Functions
Matlab and R Functions
Using interactively
R/S-Plus MATLAB/Octave Description
Rgui octave -q Start session
source('foo.R') foo(.m) Run code from file
history() history Command history
savehistory(file=".Rhistory") diary on [..] diary off Save command history
q(save='no') exit or quit End session
Operators
R/S-Plus MATLAB/Octave Description
help(Syntax) help - Help on operator syntax
Arithmetic operators
http://mathesaurus.sourceforge.net/octave-r.html Page 1 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Relational operators
R/S-Plus MATLAB/Octave Description
a == b a == b Equal
a < b a < b Less than
a > b a > b Greater than
a <= b a <= b Less than or equal
a >= b a >= b Greater than or equal
a != b a ~= b Not Equal
Logical operators
R/S-Plus MATLAB/Octave Description
a && b a && b Short-circuit logical AND
a || b a || b Short-circuit logical OR
a & b a & b or and(a,b) Element-wise logical AND
a | b a | b or or(a,b) Element-wise logical OR
xor(a, b) xor(a, b) Logical EXCLUSIVE OR
!a ~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
http://mathesaurus.sourceforge.net/octave-r.html Page 2 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Round off
R/S-Plus MATLAB/Octave Description
round(a) round(a) Round
ceil(a) ceil(a) Round up
floor(a) floor(a) Round down
fix(a) Round towards zero
Mathematical constants
R/S-Plus MATLAB/Octave Description
pi pi $\pi=3.141592$
exp(1) exp(1) $e=2.718281$
Complex numbers
R/S-Plus MATLAB/Octave Description
1i i Imaginary unit
z <- 3+4i z = 3+4i A complex number, $3+4i$
abs(3+4i) or Mod(3+4i) abs(z) Absolute value (modulus)
Re(3+4i) real(z) Real part
Im(3+4i) imag(z) Imaginary part
Arg(3+4i) arg(z) Argument
Conj(3+4i) conj(z) Complex conjugate
Trigonometry
R/S-Plus MATLAB/Octave Description
atan2(b,a) atan(a,b) Arctangent, $\arctan(b/a)$
http://mathesaurus.sourceforge.net/octave-r.html Page 3 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Vectors
R/S-Plus MATLAB/Octave Description
a <- c(2,3,4,5) a=[2 3 4 5]; Row vector, $1 \times n$-matrix
adash <- t(c(2,3,4,5)) adash=[2 3 4 5]'; Column vector, $m \times 1$-
matrix
Sequences
R/S-Plus MATLAB/Octave Description
seq(10) or 1:10 1:10 1,2,3, ... ,10
seq(0,length=10) 0:9 0.0,1.0,2.0, ... ,9.0
seq(1,10,by=3) 1:3:10 1,4,7,10
seq(10,1) or 10:1 10:-1:1 10,9,8, ... ,1
seq(from=10,to=1,by=-3) 10:-3:1 10,7,4,1
seq(1,10,length=7) linspace(1,10,7) Linearly spaced vector of n=7
points
rev(a) reverse(a) Reverse
a(:) = 3 Set all values to same scalar value
Concatenation (vectors)
R/S-Plus MATLAB/Octave Description
c(a,a) [a a] Concatenate two vectors
c(1:4,a) [1:4 a]
Repeating
R/S-Plus MATLAB/Octave Description
rep(a,times=2) [a a] 1 2 3, 1 2 3
rep(a,each=3) 1 1 1, 2 2 2, 3 3 3
rep(a,a) 1, 2 2, 3 3 3
http://mathesaurus.sourceforge.net/octave-r.html Page 4 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Vector multiplication
R/S-Plus MATLAB/Octave Description
a*a a.*a Multiply two vectors
dot(u,v) Vector dot product, $u \cdot v$
Matrices
R/S-Plus MATLAB/Octave Description
rbind(c(2,3),c(4,5)) a = [2 3;4 5] Define a matrix
array(c(2,3,4,5), dim=c(2,2))
Array creation
R/S-Plus MATLAB/Octave Description
matrix(0,3,5) or array(0,c(3,5)) zeros(3,5) 0 filled array
matrix(1,3,5) or array(1,c(3,5)) ones(3,5) 1 filled array
http://mathesaurus.sourceforge.net/octave-r.html Page 5 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Assignment
http://mathesaurus.sourceforge.net/octave-r.html Page 6 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Sum
R/S-Plus MATLAB/Octave Description
apply(a,2,sum) sum(a) Sum of each column
apply(a,1,sum) sum(a') Sum of each row
sum(a) sum(sum(a)) Sum of all elements
apply(a,2,cumsum) cumsum(a) Cumulative sum (columns)
Sorting
R/S-Plus MATLAB/Octave Description
a = [ 4 3 2 ; 2 8 6 ; 1 4 7 ] Example data
t(sort(a)) sort(a(:)) Flat and sorted
apply(a,2,sort) sort(a) Sort each column
t(apply(a,1,sort)) sort(a')' Sort each row
sortrows(a,1) Sort rows (by first row)
order(a) Sort, return indices
http://mathesaurus.sourceforge.net/octave-r.html Page 7 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Matrix manipulation
R/S-Plus MATLAB/Octave Description
a[,4:1] fliplr(a) Flip left-right
a[3:1,] flipud(a) Flip up-down
rot90(a) Rotate 90 degrees
kronecker(matrix(1,2,3),a) repmat(a,2,3) Repeat matrix: [ a a a ; a a a ]
kron(ones(2,3),a)
a[lower.tri(a)] <- 0 triu(a) Triangular, upper
a[upper.tri(a)] <- 0 tril(a) Triangular, lower
Equivalents to "size"
R/S-Plus MATLAB/Octave Description
dim(a) size(a) Matrix dimensions
ncol(a) size(a,2) or length(a) Number of columns
prod(dim(a)) length(a(:)) Number of elements
ndims(a) Number of dimensions
object.size(a) Number of bytes used in memory
http://mathesaurus.sourceforge.net/octave-r.html Page 8 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Multi-way arrays
R/S-Plus MATLAB/Octave Description
a = cat(3, [1 2; 1 2],[3 4; 3 Define a 3-way array
4]);
a(1,:,:)
Plotting
http://mathesaurus.sourceforge.net/octave-r.html Page 9 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Log plots
R/S-Plus MATLAB/Octave Description
plot(x,y, log="y") semilogy(a) logarithmic y-axis
plot(x,y, log="x") semilogx(a) logarithmic x-axis
plot(x,y, log="xy") loglog(a) logarithmic x and y axes
Functions
R/S-Plus MATLAB/Octave Description
f <- function(x) sin(x/3) - f = inline('sin(x/3) - cos(x/5)') Defining functions
cos(x/5)
plot(f, xlim=c(0,40), type='p') ezplot(f,[0,40]) Plot a function for given range
fplot('sin(x/3) - cos(x/5)',
[0,40])
% no ezplot
Polar plots
R/S-Plus MATLAB/Octave Description
theta = 0:.001:2*pi;
http://mathesaurus.sourceforge.net/octave-r.html Page 10 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
r = sin(2*theta);
polar(theta, rho)
Histogram plots
R/S-Plus MATLAB/Octave Description
hist(rnorm(1000)) hist(randn(1000,1))
hist(rnorm(1000), breaks= -4:4) hist(randn(1000,1), -4:4)
hist(rnorm(1000),
breaks=c(seq(-5,0,0.25),
seq(0.5,5,0.5)), freq=F)
plot(apply(a,1,sort),type="l") plot(sort(a))
3d data
http://mathesaurus.sourceforge.net/octave-r.html Page 11 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Data analysis
Statistics
R/S-Plus MATLAB/Octave Description
apply(a,2,mean) mean(a) Average
apply(a,2,median) median(a) Median
apply(a,2,sd) std(a) Standard deviation
apply(a,2,var) var(a) Variance
cor(x,y) corr(x,y) Correlation coefficient
cov(x,y) cov(x,y) Covariance
http://mathesaurus.sourceforge.net/octave-r.html Page 12 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Non-linear methods
Differential equations
R/S-Plus MATLAB/Octave Description
diff(a) Discrete difference function and
approximate derivative
Solve differential equations
Fourier analysis
R/S-Plus MATLAB/Octave Description
fft(a) fft(a) Fast fourier transform
fft(a, inverse=TRUE) ifft(a) Inverse fourier transform
Programming
R/S-Plus MATLAB/Octave Description
.R .m Script file extension
# % Comment symbol (rest of line)
http://mathesaurus.sourceforge.net/octave-r.html Page 13 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
% or #
library(RSvgDevice) % must be in MATLABPATH Import library functions
% must be in LOADPATH
Loops
R/S-Plus MATLAB/Octave Description
for(i in 1:5) print(i) for i=1:5; disp(i); end for-statement
for(i in 1:5) { for i=1:5 Multiline for statements
print(i) disp(i)
print(i*2) disp(i*2)
} end
Conditionals
R/S-Plus MATLAB/Octave Description
if (1>0) a <- 100 if 1>0 a=100; end if-statement
if 1>0 a=100; else a=0; end if-else-statement
ifelse(a>0,a,0) Ternary operator (if?true:false)
Debugging
R/S-Plus MATLAB/Octave Description
.Last.value ans Most recent evaluated expression
objects() whos or who List variables loaded into memory
rm(x) clear x or clear [all] Clear variable $x$ from memory
print(a) disp(a) Print
http://mathesaurus.sourceforge.net/octave-r.html Page 14 of 15
R for MATLAB users – Mathesaurus 3/30/14 11:48 PM
Permission is granted to copy, distribute and/or modify this document as long as the above attribution is retained.
http://mathesaurus.sourceforge.net/octave-r.html Page 15 of 15