\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{{[}deleted{]}} \pdfinfo{ /Title (matlab-to-r.pdf) /Creator (Cheatography) /Author ({[}deleted{]}) /Subject (Matlab TO R Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Matlab TO R Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{{[}deleted{]}} via \textcolor{DarkBackground}{\uline{cheatography.com/30394/cs/13367/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}{[}deleted{]} \\ \uline{cheatography.com/deleted-30394} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 3rd November, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{x{4.5549 cm} x{6.2419 cm} x{6.0732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Vectors, Matricies, Indexing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MATLAB}} & {\bf{R}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} a={[}1 2 3 4{]}; & a \textless{}- c(1,2,3,4) & Row vector \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} a={[}1;2;3;4{]} OR a={[}1 2 3 4{]}'; & a \textless{}- t(c(1,2,3,4)) & Column vector \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} a={[}1 2 3; 4 5 6{]}; & matrix(c(1,2,3,4,5,6),nrow=2,byrow=TRUE) & Matrix, enter by row \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} a={[}1 2 3; 4 5 6{]}; & matrix(c(1,4,2,5,3,6),nrow=2) & Matrix, enter by column \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} a(3) & a{[}3{]} & Access element \#3 \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} a={[}2:7{]}; & a \textless{}- 2:7 OR a \textless{}- c(2:7) & Vector {[}2 3 4 5 6 7{]} \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} a={[}7:-1:2{]}; & a \textless{}- 7:2 & Vector {[}7 6 5 4 3 2{]} \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} a={[}2:3:14{]}; & a \textless{}- seq(2,14,3) & Vector {[}2 5 8 11 14{]} \tn % Row Count 21 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{x=linspace(a},b,n); & x \textless{}- seq(a,b,length.out=n) OR seq(a,b,len=n) & Vector with n equally spaced values between a and b, inclusive \tn % Row Count 26 (+ 5) % Row 10 \SetRowColor{LightBackground} a=zeros(a,1) OR a=zeros(1,a) & a \textless{}- rep(0,k) & Vector of length k with all zeros \tn % Row Count 29 (+ 3) % Row 11 \SetRowColor{white} a=j{\emph{ones(a,1) OR a=j}}ones(1,a) & a \textless{}- rep(j,k) & Vecot of length k with all values j \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.5549 cm} x{6.2419 cm} x{6.0732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Vectors, Matricies, Indexing (cont)}} \tn % Row 12 \SetRowColor{LightBackground} a=zeros(m,n) & matrix(0,nrow=m,ncol=n) OR matrix(0,m,n) & Matrix of all zeros, size m by n \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} \seqsplit{a=j*ones(m},n) & matrix(j,m,n) & Matrix of all j, size m by n \tn % Row Count 5 (+ 2) % Row 14 \SetRowColor{LightBackground} a={[}a1 a2{]}; & a \textless{}- cbind(a1,a2) & "Glue" two matricies a1 and a2 together (same \# of rows) \tn % Row Count 9 (+ 4) % Row 15 \SetRowColor{white} {[}X,Y{]}=meshgrid(x,y) & m=length(x); n=length(y); \seqsplit{X=matrix(rep(x},each=n),nrow=n); \seqsplit{Y=matrix(rep(y},m),nrow=n) & X rows are copies of x, Y columns are copies of y \tn % Row Count 15 (+ 6) % Row 16 \SetRowColor{LightBackground} A(:,2) & A{[},2{]} & Column 2 of matrix A \tn % Row Count 17 (+ 2) % Row 17 \SetRowColor{white} A(7,:) & A{[}7,{]} & Row 7 of matrix A \tn % Row Count 19 (+ 2) % Row 18 \SetRowColor{LightBackground} fliplr(A) & t(apply(A,1,rev)) & Flip the order of elements in each row of matrix A \tn % Row Count 23 (+ 4) % Row 19 \SetRowColor{white} flipud(A) & apply(A,2,rev) & Flip the order of elements in each column of matrix A \tn % Row Count 27 (+ 4) % Row 20 \SetRowColor{LightBackground} v(a:end) & v{[}a:length(v){]} & Extract elements of v from positon a to end \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.5549 cm} x{6.2419 cm} x{6.0732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Vectors, Matricies, Indexing (cont)}} \tn % Row 21 \SetRowColor{LightBackground} No simple way & v{[}c(-j,-k){]} & All but the jth and kth elements of v \tn % Row Count 3 (+ 3) % Row 22 \SetRowColor{white} A = reshape(A,m,n) & dim(A) \textless{}- c(m,n) & Reshape matrix A into an m by n matrix (take elements columnwise from original matrix A) \tn % Row Count 10 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Cell Array/List}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MATLAB}} & {\bf{R}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} v=cell(1,n); v\{1\}=12; v\{2\}='hi there'; v\{3\}=rand(3); & v \textless{}- \seqsplit{vector('list'},n); v{[}{[}1{]}{]}=12; v{[}{[}2{]}{]}='hi there'; v{[}{[}3{]}{]} = \seqsplit{matrix(runif(9)},3) & Vector of length n capable of containing different data types in different elements (aka cell or list) \tn % Row Count 10 (+ 8) % Row 2 \SetRowColor{LightBackground} w=v\{i\} & w=v{[}{[}i{]}{]} & Extract the ith element of cell/list vector \tn % Row Count 14 (+ 4) % Row 3 \SetRowColor{white} No names associated with elements of cell arrays & names(v){[}3{]} \textless{}- \seqsplit{'myrandmatrix'} & Set the name of the ith element in a list \tn % Row Count 18 (+ 4) % Row 4 \SetRowColor{LightBackground} No names associated with elements of cell arrays & names(v) & See all names of elements in list \tn % Row Count 22 (+ 4) % Row 5 \SetRowColor{white} No names associated with elements of cell arrays & names(v) = NULL & Clear all names \tn % Row Count 26 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Structures/Data Frames}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{avals=2*ones(1},6); \seqsplit{yvals=6:-1:1;} v={[}1 5 3 2 3 7{]}; d = struct('a',avals,'y',yvals,'fac',v); & v \textless{}- c(1,5,3,2,3,7); d \textless{}- \seqsplit{data.frame(cbind(a=2}, y=6:1,v)) & Create a matrix-like object with different named columns ({\emph{structure}} in MATLAB, {\emph{data frame}} in R) \tn % Row Count 8 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.3984 cm} x{5.3984 cm} x{6.0732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Basic Computation}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MATLAB}} & {\bf{R}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} a=1; b=2; & a \textless{}- 1; b \textless{}- 2; & Variable assignment \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} a+b; a*b; a\textasciicircum{}b; etc & a+b; a*b; a\textasciicircum{}b; etc. & Add, subtract, multiply, divide, power \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} -{}- & a \%/\% b & Integer division \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} abs(a); exp(a); log(a); log10(a); & abs(a); exp(a); log(a); log10(a); & absolute value, e, ln, log base 10 \tn % Row Count 12 (+ 3) % Row 5 \SetRowColor{white} sin(a); asin(a); sinh(a); asinh(a); & sin(a); asin(a); sinh(a); asinh(a); & sin, inverse sin, hyperbolic sin, inverse hyperbolic sin \tn % Row Count 16 (+ 4) % Row 6 \SetRowColor{LightBackground} mod(n,k) & n \%\% k & Remainder \tn % Row Count 17 (+ 1) % Row 7 \SetRowColor{white} round(x), floor(x), ceil(x) & round(x), floor(x), ceil(x) & Round, round down, round up \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} sign(x) & sign(x) & Sign of x (+1, 0, or -1) \tn % Row Count 22 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.2053 cm} x{6.9167 cm} x{6.748 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Matrix Computations}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MATLAB}} & {\bf{R}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} dot(x,y) & sum(x*y) & Vector dot product \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{cross(x},y) & xprod in RSEIS package & Vector cross product \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} A*B & A \%*\% B & Matrix multiplication AB \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} A.*B & A*B & \seqsplit{Element-by-element} multiplication of A and B \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} A\textbackslash{}b & solve(A,b) & Solve Ax=b \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{mean(v)}, \seqsplit{mean(A(:))} & mean(v) or mean(A) & Mean of all elements in vector or matrix \tn % Row Count 15 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{mean(A)}, sum(A) & colMeans(A), colSums(A) & Means or sums of columns in a matrix \tn % Row Count 18 (+ 3) % Row 8 \SetRowColor{LightBackground} mean(A,2), sum(A,2) & rowMeans(A), rowSums(A) & Means or sums of rows in a matrix \tn % Row Count 21 (+ 3) % Row 9 \SetRowColor{white} std(v), \seqsplit{std(A(:))} & sd(v), sd(c(A)) & Standard deviation of all elements in a vector of matrix, normalized by (n-1) \tn % Row Count 26 (+ 5) % Row 10 \SetRowColor{LightBackground} std(A) & sd(A) & Standard deviations of columns of a matrix \tn % Row Count 29 (+ 3) % Row 11 \SetRowColor{white} std(A,2) & apply(A,1,sd) & Standard deviations of rows of a matrix \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.2053 cm} x{6.9167 cm} x{6.748 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Matrix Computations (cont)}} \tn % Row 12 \SetRowColor{LightBackground} min(v), \seqsplit{min(A(:))} & min(v), min(A) & Minimum of all elements in vector or matrix \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} min(A) & apply(A,2,min) & Minimum value in each column of A \tn % Row Count 6 (+ 3) % Row 14 \SetRowColor{LightBackground} min(A,{[}{]},2) & apply(A,1,min) & Minimum value in each row of matrix A \tn % Row Count 9 (+ 3) % Row 15 \SetRowColor{white} min(A,c) & pmin(A,c) & Given matrix A and scalar c, compute a matrix where each element is the minimum of c and corresponding element of A \tn % Row Count 17 (+ 8) % Row 16 \SetRowColor{LightBackground} {[}y,ind{]}=min(v) & ind = which.min(v) & Find index of the first time min(v) appears in v and store that index as ind \tn % Row Count 22 (+ 5) % Row 17 \SetRowColor{white} size(A,1) & nrow(A) & Number of rows in A \tn % Row Count 24 (+ 2) % Row 18 \SetRowColor{LightBackground} size(A,2) & ncol(A) & Number of columns in A \tn % Row Count 26 (+ 2) % Row 19 \SetRowColor{white} \seqsplit{size(A)} & dim(A) & Dimensions of A, listed as a vector \tn % Row Count 29 (+ 3) % Row 20 \SetRowColor{LightBackground} \seqsplit{length(v)} & length(v) & Number of elements in vector \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.2053 cm} x{6.9167 cm} x{6.748 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Matrix Computations (cont)}} \tn % Row 21 \SetRowColor{LightBackground} \seqsplit{numel(A)} & length(A) & Number of elements in matrix \tn % Row Count 2 (+ 2) % Row 22 \SetRowColor{white} \seqsplit{sort(v)} & sort(v) & Sort values in vector v \tn % Row Count 4 (+ 2) % Row 23 \SetRowColor{LightBackground} {[}s,idx{]}=sort(v) & tmp \textless{}- sort(v,index,return=TRUE); s \textless{}- 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{]}{]} \tn % Row Count 11 (+ 7) % Row 24 \SetRowColor{white} find(v\textgreater{}5) & which(v\textgreater{}5) & List of indicies of each element of v which are greater than 5 \tn % Row Count 15 (+ 4) % Row 25 \SetRowColor{LightBackground} {[}r,c{]} = find(A\textgreater{}5) & w \textless{}- which(A\textgreater{}5, arr.ind=TRUE); r \textless{}- w{[},1{]}; c \textless{}- w{[},2{]}; & Generate r and c giving rows and columns of elements of A which are greater than 5 \tn % Row Count 21 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.2419 cm} x{6.0732 cm} x{4.5549 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Relationals and Logicals}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MATLAB}} & {\bf{R}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} a == b; a \textless{} b; a \textgreater{}= b; etc. & a == b; a \textless{} b; a \textgreater{}= b; etc. & eq, gt, lt, gte, lte \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} a \textasciitilde{}= b & a != b & Not equal \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} a \&\& b; a || b; & a \&\& b; a || b; & AND, OR \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \textasciitilde{}a & !a & Not \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.8801 cm} x{5.5671 cm} x{7.4228 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{GUI Commands}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{MATLAB}} & {\bf{R}} & {\bf{Description}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} clear all & \seqsplit{rm(list=ls())} & Clear all variables \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} close all & \seqsplit{graphics.off()} & Close all figures \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} clc & ctrl+L & Clear console \tn % Row Count 7 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \end{document}