Show Menu
Cheatography

Matlab TO R Cheat Sheet (DRAFT) by [deleted]

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Vectors, Matricies, Indexing

MATLAB
R
Descri­ption
a=[1 2 3 4];
a <- c(1,2,3,4)
Row vector
a=[1;2­;3;4] OR a=[1 2 3 4]';
a <- t(c(1,­2,3,4))
Column vector
a=[1 2 3; 4 5 6];
matrix­(c(­1,2­,3,­4,5­,6)­,nr­ow=­2,b­yro­w=TRUE)
Matrix, enter by row
a=[1 2 3; 4 5 6];
matrix­(c(­1,4­,2,­5,3­,6)­,nr­ow=2)
Matrix, enter by column
a(3)
a[3]
Access element #3
a=[2:7];
a <- 2:7 OR a <- c(2:7)
Vector [2 3 4 5 6 7]
a=[7:-­1:2];
a <- 7:2
Vector [7 6 5 4 3 2]
a=[2:3­:14];
a <- seq(2,­14,3)
Vector [2 5 8 11 14]
x=lins­pac­e(a­,b,n);
x <- seq(a,­b,l­eng­th.o­ut=n) OR seq(a,­b,l­en=n)
Vector with n equally spaced values between a and b, inclusive
a=zero­s(a,1) OR a=zero­s(1,a)
a <- rep(0,k)
Vector of length k with all zeros
a=jones(a,1) OR a=jones(1,a)
a <- rep(j,k)
Vecot of length k with all values j
a=zero­s(m,n)
matrix­(0,­nro­w=m­,nc­ol=n) OR matrix­(0,m,n)
Matrix of all zeros, size m by n
a=j*on­es(m,n)
matrix­(j,m,n)
Matrix of all j, size m by n
a=[a1 a2];
a <- cbind(­a1,a2)
"­Glu­e" two matricies a1 and a2 together (same # of rows)
[X,Y]=­mes­hgr­id(x,y)
m=leng­th(x); n=leng­th(y); X=matr­ix(­rep­(x,­eac­h=n­),n­row=n); Y=matr­ix(­rep­(y,­m),­nrow=n)
X rows are copies of x, Y columns are copies of y
A(:,2)
A[,2]
Column 2 of matrix A
A(7,:)
A[7,]
Row 7 of matrix A
fliplr(A)
t(appl­y(A­,1,­rev))
Flip the order of elements in each row of matrix A
flipud(A)
apply(­A,2­,rev)
Flip the order of elements in each column of matrix A
v(a:end)
v[a:le­ngt­h(v)]
Extract elements of v from positon a to end
No simple way
v[c(-j­,-k)]
All but the jth and kth elements of v
A = reshap­e(A­,m,n)
dim(A) <- c(m,n)
Reshape matrix A into an m by n matrix (take elements columnwise from original matrix A)

Cell Array/List

MATLAB
R
Descri­ption
v=cell­(1,n); v{1}=12; v{2}='hi there'; v{3}=r­and(3);
v <- vector­Â­('­l­i­st­­',n); v[[1]]=12; v[[2]]='hi there'; v[[3]] = matrix­Â­(r­u­n­if­­(9),3)
Vector of length n capable of containing different data types in different elements (aka cell or list)
w=v{i}
w=v[[i]]
Extract the ith element of cell/list vector
No names associated with elements of cell arrays
names(­Â­v)[3] <- 'myran­Â­dm­a­trix'
Set the name of the ith element in a list
No names associated with elements of cell arrays
names(v)
See all names of elements in list
No names associated with elements of cell arrays
names(v) = NULL
Clear all names

Struct­ure­s/Data Frames

avals=­2*o­nes­(1,6); yvals=­6:-1:1; v=[1 5 3 2 3 7]; d = struct­('a­',a­val­s,'­y',­yva­ls,­'fa­c',v);
v <- c(1,5,­3,2­,3,7); d <- data.f­ram­e(c­bin­d(a=2, y=6:1,v))
Create a matrix­-like object with different named columns (structure in MATLAB, data frame in R)

Basic Comput­ation

MATLAB
R
Descri­ption
a=1; b=2;
a <- 1; b <- 2;
Variable assignment
a+b; a*b; a^b; etc
a+b; a*b; a^b; etc.
Add, subtract, multiply, divide, power
--
a %/% b
Integer division
abs(a); exp(a); log(a); log10(a);
abs(a); exp(a); log(a); log10(a);
absolute value, e, ln, log base 10
sin(a); asin(a); sinh(a); asinh(a);
sin(a); asin(a); sinh(a); asinh(a);
sin, inverse sin, hyperbolic sin, inverse hyperbolic sin
mod(n,k)
n %% k
Remainder
round(x), floor(x), ceil(x)
round(x), floor(x), ceil(x)
Round, round down, round up
sign(x)
sign(x)
Sign of x (+1, 0, or -1)

Matrix Comput­ations

MATLAB
R
Descri­ption
dot(x,y)
sum(x*y)
Vector dot product
cross(x,y)
xprod in RSEIS package
Vector cross product
A*B
A %*% B
Matrix multip­lic­ation AB
A.*B
A*B
Elemen­t-b­y-e­lement multip­lic­ation of A and B
A\b
solve(A,b)
Solve Ax=b
mean(v), mean(A(:))
mean(v) or mean(A)
Mean of all elements in vector or matrix
mean(A), sum(A)
colMea­ns(A), colSums(A)
Means or sums of columns in a matrix
mean(A,2), sum(A,2)
rowMea­ns(A), rowSums(A)
Means or sums of rows in a matrix
std(v), std(A(:))
sd(v), sd(c(A))
Standard deviation of all elements in a vector of matrix, normalized by (n-1)
std(A)
sd(A)
Standard deviations of columns of a matrix
std(A,2)
apply(­A,1,sd)
Standard deviations of rows of a matrix
min(v), min(A(:))
min(v), min(A)
Minimum of all elements in vector or matrix
min(A)
apply(­A,2­,min)
Minimum value in each column of A
min(A,­[],2)
apply(­A,1­,min)
Minimum value in each row of matrix A
min(A,c)
pmin(A,c)
Given matrix A and scalar c, compute a matrix where each element is the minimum of c and corres­ponding element of A
[y,ind­]=m­in(v)
ind = which.m­in(v)
Find index of the first time min(v) appears in v and store that index as ind
size(A,1)
nrow(A)
Number of rows in A
size(A,2)
ncol(A)
Number of columns in A
size(A)
dim(A)
Dimensions of A, listed as a vector
length(v)
length(v)
Number of elements in vector
numel(A)
length(A)
Number of elements in matrix
sort(v)
sort(v)
Sort values in vector v
[s,idx­]=s­ort(v)
tmp <- sort(v­,in­dex­,re­tur­n=T­RUE); s <- tmp$s; idx=tmp$ix
Sort values in v, putting sorted values in s and indicies in idx, in teh sense that s[k] = x[idx[k]]
find(v­>5)
which(­v>5)
List of indicies of each element of v which are greater than 5
[r,c] = find(A­>5)
w <- which(­A>5, arr.in­d=T­RUE); r <- w[,1]; c <- w[,2];
Generate r and c giving rows and columns of elements of A which are greater than 5

Relati­onals and Logicals

MATLAB
R
Descri­ption
a == b; a < b; a >= b; etc.
a == b; a < b; a >= b; etc.
eq, gt, lt, gte, lte
a ~= b
a != b
Not equal
a && b; a || b;
a && b; a || b;
AND, OR
~a
!a
Not

GUI Commands

MATLAB
R
Descri­ption
clear all
rm(lis­t=ls())
Clear all variables
close all
graphi­cs.o­ff()
Close all figures
clc
ctrl+L
Clear console