% ============================================================================
%  PROJECT : MATLAB Reference Sheet
%  FILE    : MATLAB_Reference_Sheet.tex
%  VERSION : 1.0
%  DATE    : January 22, 2006
%  AUTHOR  : Giordano Fusco
%  E-MAIL  : fusco@cs.wisc.edu
%
%  ABSTRACT:
%    MATLAB Reference Sheet.
%
%  HISTORY:
%    Nothing relevant.
%
%  COPYRIGHT:
%    Copyright (C) 2006, Giordano Fusco.
%
%  NO WARRANTY:
%    This program is free software; you can redistribute it and/or
%    modify it under the terms of the GNU General Public License
%    as published by the Free Software Foundation; either version 2
%    of the License, or any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program; if not, write to the Free Software
%    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
%    USA.
% ============================================================================



\documentclass[oneside,twocolumns,9pt]{article}



% ============================================================================
%  Title
% ============================================================================
\title{\vspace*{-30pt}\framebox[\textwidth]
  {\begin{tabular}[b]{@{}c@{}}
      \Large{CS 412} \\[-13pt]
      \scriptsize{UW-Madison}
    \end{tabular}
    \hfill \Huge{\textbf{MATLAB Reference Sheet}} \hfill
    \scriptsize{\begin{tabular}[b]{@{}c@{}}
        created by \\
        Giordano Fusco, \\
        version 01-22-06
      \end{tabular}}
  }}
\author{\vspace*{-3.5\baselineskip}}
\date{\vspace*{-3.5\baselineskip}}



% ============================================================================
%  Settings
% ============================================================================
\setlength{\parindent}{0pt}
\setlength{\arraycolsep}{2pt}

\setlength{\voffset}{-25pt}
\setlength{\topmargin}{0pt}
\setlength{\headheight}{0pt}
\setlength{\headsep}{0pt}
\setlength{\footskip}{0pt}
\setlength{\textheight}{\paperheight}
\addtolength{\textheight}{-2in}
\addtolength{\textheight}{120pt}

\setlength{\hoffset}{-30pt}
\setlength{\oddsidemargin}{-5pt}
\setlength{\evensidemargin}{-5pt}
\setlength{\textwidth}{\paperwidth}
\addtolength{\textwidth}{-2in}
\addtolength{\textwidth}{60pt}

\newenvironment{TableShort}
{\begin{tabular}{@{}p{0.05\columnwidth}p{0.9\columnwidth}@{}}}{\end{tabular}}
\newenvironment{Table}
{\begin{tabular}{@{}p{0.12\columnwidth}p{0.83\columnwidth}@{}}}{\end{tabular}}
\newenvironment{TableLarge}
{\begin{tabular}{@{}p{0.19\columnwidth}p{0.76\columnwidth}@{}}}{\end{tabular}}

\newcommand{\m}[1]{\texttt{#1}}
\newcommand{\help}[1]{\small{(\texttt{help #1})}}

\newcommand{\SectionShort}[1]{\vspace*{-4pt}\section*{#1}\vspace*{-4pt}}
\newcommand{\Section}[1]{\section*{#1}}
\newcommand{\SectionLarge}[1]{\vspace*{4pt}\section*{#1}\vspace*{2pt}}



% ============================================================================
%  Begin document
% ============================================================================
\begin{document}

\twocolumn[\maketitle]

\pagestyle{empty}
\thispagestyle{empty}
\small



% ----------------------------------------------------------------------------
%  Useful Workspace functions
% ----------------------------------------------------------------------------
\Section{Useful workspace functions \help{general}}

\begin{Table}
  \m{help}    & obtain help generally or for a specific function \\
  \m{lookfor} & obtain one-line help if it exists \\
  \m{more}    & toggles pagination, useful for longs ``helps'' \\
  \m{load}    & read variables from a file \\
  \m{save}    & save all or selected variables to a file \\
\end{Table}



% ----------------------------------------------------------------------------
%  MATLAB path and environment
% ----------------------------------------------------------------------------
\SectionLarge{Path and environment \help{general}}

\begin{Table}
  \m{cd}   & change to a specific directory \\
  \m{dir}  & list files in the current directory \\
  \m{path} & display or modify the function search path \\
\end{Table}



% ----------------------------------------------------------------------------
%  MATLAB types
% ----------------------------------------------------------------------------
\SectionLarge{Basic types}

The basic variable type is a two-dimensional array of doubles
(64-bit representation). \\
A \emph{scalar} is a $1 \times 1$ array. \\
A \emph{row vector} of length $n$ is a $1 \times n$ array. \\
A \emph{column vector} of length m is an $m \times 1$ array. \\
A \emph{matrix} of dimensions $m$ rows and $n$ columns is an $m \times
n$ array.



% ----------------------------------------------------------------------------
%  Variable name conventions
% ----------------------------------------------------------------------------
\SectionLarge{Variable name conventions}

MATLAB is case sensitive. \\
A variable must start with a letter (A-Z, a-z). \\
Up to 31 letters, digits and underscores.



% ----------------------------------------------------------------------------
%  Default MATLAB variables/constants
% ----------------------------------------------------------------------------
\SectionLarge{Default variables/constants \help{elmat}}

\begin{Table}
  \m{ans}      & result of last unassigned calculation \\
  \m{eps}      & smallest number that can be added to 1.0 and still be
                 different \\
  \m{flops}    & count of floating point operations \\
  \m{Inf}      & infinity, e.g. 1/0 = \m{Inf} \\
  \m{NaN}      & Not a Number, e.g. 0/0 = \m{NaN} \\
  \m{pi}       & value of $\pi$ ($3.1415\ldots$) \\
  \m{i}, \m{j} & $\sqrt{-1}$ \\
  \m{realmax}  & largest real number MATLAB can represent \\
  \m{realmin}  & smallest real number MATLAB can represent \\
\end{Table}



% ----------------------------------------------------------------------------
%  About user defined variables
% ----------------------------------------------------------------------------
\SectionLarge{About user variables \help{elmat,help general}}

\begin{Table}
  \m{clear}  & clear all or selected variables (or functions)
                from the current workspace \\
  \m{length} & length of a vector or maximum dimension of an array \\
  \m{size}   & display dimensions of a particular array \\
  \m{who}    & display current workspace variable names \\
  \m{whos}   & display current workspace variable names,
               types and associated sizes \\
\end{Table}



% ----------------------------------------------------------------------------
%  Formatting
% ----------------------------------------------------------------------------
\SectionLarge{Formatting \help{format}}

\begin{tabular}{@{}p{0.25\columnwidth}p{0.7\columnwidth}@{}}
  \m{format short} & scaled fixed point format with 5 digits \\
  \m{format long}  & scaled fixed point format with 15 digits \\
  \mbox{\m{format compact}} & suppress extra line-feeds \\
  \m{format loose} & put extra line-feeds in the output \\
\end{tabular}



% ----------------------------------------------------------------------------
%  MATLAB punctuation
% ----------------------------------------------------------------------------
\Section{Punctuation \help{punct}}

\begin{TableShort}
  \m{.}      & decimal point, e.g. 325/100, 3.25 and .325e1 are equivalent \\
  \m{\ldots} & three or more decimal points at the end of a line cause
               the following line to be a continuation \\
  \m{,}      & comma is used to separate matrix elements and arguments to
               functions, also used to separate statements in
               multi-statement lines \\
  \m{;}      & used inside brackets to indicate the ends of the rows of a
               matrix, also used after an expression or statement
               to suppress printing \\
  \m{\%}     & begins comments \\
  \m{{'}}    & quote. \m{{'}ANY TEXT{'}} is a vector whose
               components are the ASCII codes for the characters. A quote
               within the text is indicated by two quotes, e.g.
               \m{{'}Don{'}{'}t forget.{'}} \\
\end{TableShort}



% ----------------------------------------------------------------------------
%  Explicit matrix creation
% ----------------------------------------------------------------------------
\SectionShort{Explicit matrix creation}

Elements in a row can be delimited by a comma or a space. \\
Explicit assignment using ;'s to end rows \\
\hspace*{15pt}\m{a = [1,2,3;4,5,6;7,8,9]} \\
Explicit assignment using ``newline'' to end rows \\
\hspace*{15pt}\m{a = [1,2,3 \\
\hspace*{15pt}4,5,6 \\
\hspace*{15pt}7,8,9]} \\
Explicit assignment using continuation lines \\
\hspace*{15pt}\m{b = [1 2 3 4 5 6 \ldots \\
\hspace*{15pt}7 8 9 10]}



% ----------------------------------------------------------------------------
%  Vector/Matrix initialization
% ----------------------------------------------------------------------------
\SectionShort{Vector/Matrix initialization  \help{elmat}}

\begin{TableLarge}
  \m{linspace(a,b,N)} & \hspace*{25pt} linearly spaced intervals between
                        $a$ and $b$ (inclusive) comprised of N points \\
  \m{zeros(m,n)}      & an $m$ by $n$ array of zeroes \\
  \m{zeros(n)}        & an $n$ by $n$ array of zeroes \\
  \m{ones(m,n)}       & an $m$ by $n$ array of ones \\
  \m{ones(n)}         & an $n$ by $n$ array of ones \\
  \m{eye(m,n)}        & an $m$ by $n$ array with ones on the diagonal \\
  \m{eye(n)}          & an $n$ by $n$ identity matrix \\
  \m{ones(n)}         & an $n$ by $n$ array of ones \\
  \m{rand(m,n)}       & an $m$ by $n$ array of random numbers \\
  \m{rand(n)}         & an $n$ by $n$ array of random numbers \\
\end{TableLarge}



% ----------------------------------------------------------------------------
%  List generation/variable indexing
% ----------------------------------------------------------------------------
\SectionShort{List generation/variable indexing}

\begin{TableLarge}
  \m{i:k:l}      & list generation: $1stValue:Stride:LastValue$ \\
  \m{v(1)}       & 1st element of vector $v$ \\
  \m{v(end)}     & last element of vector $v$ \\
  \m{v(1:2:9)}   & 1st, 3rd, 5th, 7th, 9th elements of vector $v$ \\
  \m{v(2:3:9)}   & 2nd, 5th, 8th elements of vector $v$ \\
  \m{A(2,3)}     & 2'nd row, 3'rd column of matrix $A$ \\
  \m{A(:,3)}     & all elements in column 3 \\
  \m{A(1,:)}     & all elements in row 1 \\
  \mbox{\m{A(1:2:end,:)}} & all odd rows of matrix $A$ \\
  \m{A(1:2,2:4)} & sub-matrix of rows 1 and 2, columns 2 through 4 \\
  \m{A(1,end)}   & last element in 1'st row \\
\end{TableLarge}



% ----------------------------------------------------------------------------
%  Vector/Matrix operations
% ----------------------------------------------------------------------------
\SectionShort{Vector/Matrix op's \help{arith, help ops}}

\begin{tabular}{@{}p{0.05\columnwidth}p{0.3\columnwidth}
                   p{0.05\columnwidth}p{0.45\columnwidth}@{}}
  \m{+}               & addition \\
  \m{-}               & subtraction \\
  \m{*}               & multiplication &
  \m{.*}              & point-wise multiplication \\
  \m{/}               & left division  &
  \m{./}              & point-wise left division  \\
  $\backslash$        & right division &
  \m{.}$\backslash$   & point-wise right division \\
  $\;\widehat{}$      & exponentiation &
  \m{.}$\;\widehat{}$ & point-wise exponentiation \\
  \m{'}               & transpose \\
\end{tabular}



% ----------------------------------------------------------------------------
%  Loops
% ----------------------------------------------------------------------------
\Section{Loops \help{lang}}

\m{for k = $vectorOrColumnList$ \\
  \hspace*{15pt} \% MATLAB statements \\
  end}

\bigskip

\m{while $logicalExpression$ \\
  \hspace*{15pt} \% MATLAB statements \\
  end
}

\bigskip

Note that MATLAB is an interpreted language, and hence loops are
slower than internal vector manipulation function. So it is better to
avoid loops whenever possible.



% ----------------------------------------------------------------------------
%  if/elseif/else construct
% ----------------------------------------------------------------------------
\SectionShort{if/elseif/else construct \help{lang}}

\m{if $logicalExpression1$ \quad \% Mandatory \\
\hspace*{15pt} \% MATLAB statements \\
elseif $logicalExpression2$ \quad \% Optional \\
\hspace*{15pt} \% MATLAB statements \\
elseif $logicalExpression3$ \quad \% Optional \\
\hspace*{15pt} . \\
\hspace*{15pt} . \\
elseif $logicalExpressionN$ \quad \% Optional \\
\hspace*{15pt} \% MATLAB statements \\
else \quad \% Optional \\
\hspace*{15pt} \% MATLAB statements \\
end \quad \% Mandatory}



% ----------------------------------------------------------------------------
%  Logical operators
% ----------------------------------------------------------------------------
\SectionShort{Logical operators \help{relop}}

\begin{TableShort}
  \m{<}       & less than \\
  \m{<=}      & less than or equal \\
  \m{>}       & greater than \\
  \m{>=}      & greater than or equal \\
  \m{==}      & equal \\
  \m{$\sim$=} & not equal \\
  \m{\&}      & logical AND \\
  \m{|}       & logical OR \\
  \m{$\sim$}  & logical NOT \\
\end{TableShort}



% ----------------------------------------------------------------------------
%  Script M-files
% ----------------------------------------------------------------------------
\SectionShort{Script M-files}

Sequences of MATLAB commands can be stored in text files with the
extension \m{.m}. The commands can be executed by typing the name of
the files (without the extension) or through the file management tools
provided by the Command Window menu.



% ----------------------------------------------------------------------------
%  Function M-files
% ----------------------------------------------------------------------------
\SectionShort{Function M-files}

Define a separate file called \m{\emph{functionName}.m} with the
following form: \\
\m{function [out1,...,outN] = \emph{functionName}(in1,...inM) \\
  \% functionName:\ A brief one line description (optional) \\
  \% . \\
  \% . \\
  \% More description (optional) \\
  \% . \\
  \% . \\
  \hspace*{15pt} \% First executable statement \\
  \hspace*{15pt} . \\
  \hspace*{15pt} . \\
  \hspace*{15pt} \% Valid executable MATLAB statements and comments \\
  \hspace*{15pt} . \\
  \hspace*{15pt} . \\
  \hspace*{15pt} \% Last line}

\bigskip

The function call is made with the following statement: \\
\m{[out1,out2,...,outN] = \emph{functionName}(in1,in2,...inM)}



% ----------------------------------------------------------------------------
%  Useful in M-files
% ----------------------------------------------------------------------------
\Section{Useful in M-files \help{general, help lang}}

\begin{Table}
  \m{disp}     & display a string \\
  \m{fprintf}  & write data to screen of file \\
  \m{echo}     & toggle command echo \\
  \m{error}    & display message and abort \\
  \m{input}    & prompt for input \\
  \m{keyboard} & transfer control to keyboard \\
  \m{pause}    & wait for time or user response \\
  \m{return}   & return to caller \\
  \m{warning}  & display warning messages \\
\end{Table}



% ----------------------------------------------------------------------------
%  Figure Window control
% ----------------------------------------------------------------------------
\Section{Figure window control \help{graphics}}

\begin{TableLarge}
  \m{clc}       & clear the command window \\
  \m{clf}       & clear the figure window \\
  \m{figure}    & start a new figure window \\
  \m{figure(n)} & make figure with index $n$ active. If $n$ is an
                  integer and figure$(n)$ does not exist, create it \\
  \m{close}     & close current figure window \\
  \m{close(n)}  & close figure with index $n$ \\
  \mbox{\m{print -dpdf \emph{fileName}.pdf}} &
                  \hspace*{55pt} save the current figure in a pdf file \\
\end{TableLarge}



% ----------------------------------------------------------------------------
%  Plotting
% ----------------------------------------------------------------------------
\Section{Plotting \help{graph2d, help graph3d}}

\begin{Table}
  \m{contour}  & contour plot on a plane \\
  \m{contour3} & 3-D contour plot with displayed depth \\
  \m{mesh}     & 3-D mesh surface \\
  \m{meshc}    & combination mesh/contour plot \\
  \m{meshz}    & 3-D mesh with curtain \\
  \m{pcolor}   & pseudocolor (checkerboard) plot \\
  \m{plot}     & basic 2D plots \\
  \m{plot3}    & plot lines and points in 3-D space \\
  \m{surf}     & 3-D colored surface \\
  \m{surfc}    & combination surf/contour plot \\
  \m{surfl}    & 3-D shaded surface with lighting \\
\end{Table}



% ----------------------------------------------------------------------------
%  Plotting annotation
% ----------------------------------------------------------------------------
\Section{Plotting annotation \help{graph2d, graph3d}}

\begin{Table}
  \m{clabel}   & contour plot elevation labels \\
  \m{colorbar} & display color bar (color scale) \\
  \m{legend}   & graph legend \\
  \m{title}    & graph title \\
  \m{xlabel}   & x-axis label \\
  \m{ylabel}   & y-axis label \\
\end{Table}



% ----------------------------------------------------------------------------
%  More about plotting
% ----------------------------------------------------------------------------
\Section{More about plotting \help{graph2d, graph3d}}

\begin{Table}
  \m{box}      & toggle the box display \\
  \m{colormap} & color look-up table \\
  \m{grid}     & toggle the grid state \\
  \m{hold}     & control multiple plots on a single figure \\
  \m{shading}  & color shading mode, e.g. \m{flat}, \m{interp} \\
  \m{subplot}  & control multiple plots in one window \\
  \m{zoom}     & enable mouse-based zooming \\
\end{Table}



% ----------------------------------------------------------------------------
%  Math functions
% ----------------------------------------------------------------------------
\Section{Math functions \help{elfun, datafun, matfun}}

The following functions have their intuitive standard meaning: \\
\m{abs}, \m{exp}, \m{log}, \m{log10}, \m{log2}, \m{sqrt}, \m{sin},
\m{asin}, \m{cos}, \m{acos}, \m{tan}, \m{atan}, \m{floor}, \m{ceil},
\m{round}, \m{max}, \m{min}, \m{mean}, \m{median}, \m{norm}, \m{rank},
\m{det}, \m{inv}, \m{sort}.



% ----------------------------------------------------------------------------
%  Performance monitoring
% ----------------------------------------------------------------------------
\Section{Performance monitoring \help{timefun}}

\begin{TableLarge}
  \m{tic}, \m{toc} & stopwatch timer functions \\
  \m{flops}        & counts floating point operations \\
\end{TableLarge}



% ============================================================================
%  End document
% ============================================================================
\end{document}



% ============================================================================
%  Templates
% ============================================================================

% ----------------------------------------------------------------------------
%  
% ----------------------------------------------------------------------------
\section{}

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%  
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\subsection{}

% -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
%  
% -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
\paragraph{}

