matlab & latex tutorial

18
Matlab & LaTeX Tutorial Course web page: vision.cis.udel.edu/cv February 14, 2003 Lecture 2

Upload: landry

Post on 19-Jan-2016

61 views

Category:

Documents


3 download

DESCRIPTION

Matlab & LaTeX Tutorial. Course web page: vision.cis.udel.edu/cv. February 14, 2003  Lecture 2. Announcements. Try running Matlab, pdflatex; let me know if you’re having any trouble getting an account or otherwise - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Matlab & LaTeX Tutorial

Matlab & LaTeXTutorial

Course web page:vision.cis.udel.edu/cv

February 14, 2003 Lecture 2

Page 2: Matlab & LaTeX Tutorial

Announcements

• Try running Matlab, pdflatex; let me know if you’re having any trouble getting an account or otherwise

• Read about 3-D geometry in Chapter 2-2.1 of Forsyth & Ponce for Monday’s lecture

Page 3: Matlab & LaTeX Tutorial

Outline

• Matlab– A high-level language for matrix

calculations, numerical analysis, & scientific computing

• LaTeX– A set of typesetting macros for

documents containing equations, specialized mathematical symbols, etc.

Page 4: Matlab & LaTeX Tutorial

Matlab

• For solving problems that arise in physics, engineering, and other sciences such as integration, differential equations, and optimization numerically as opposed to analytically (like Mathematica)

• Toolboxes are libraries for particular applications such as images, fluid dynamics, etc.

• Language features– Intepreted– No variable declarations– Automatic memory management – Variable argument lists control function behavior– Vectorized: Can use for loops, but largely unnecessary

Page 5: Matlab & LaTeX Tutorial

How to Get Help

• In Matlab – Type “help” to get a listing of topics– “help <topic>” gets help for that topic.

The information is at the level of a Unix man page

• On the web– “Matlab links” on course web page has

pointers– Especially MathWorks help desk: www.mathworks.com/access/helpdesk/help/helpdesk.shtml

Page 6: Matlab & LaTeX Tutorial

Entering Variables

• Entering a scalar, vector, matrix>> a = 27;>> V = [10, 4.5, a];>> M = [3, 4 ; -6, 5];

• Without semi-colon, input is echoed (this is bad when you’re loading images!). You can use this as a kind of print statement, though

• Comma to separate statements on same line• size: Number of rows, columns

Page 7: Matlab & LaTeX Tutorial

Constructing Matrices

• Basic built-ins:– All zeroes, ones: zeros, ones– Identity: eye– Random: rand (uniform), randn (unit normal)

• Ranges: m:n, m:i:n (i is step size)• Composing big matrices out of small matrix

blocks• repmat(A, m, n): “Tile” a big matrix with m x n

copies of A• Loading data: M = dlmread(‘filename’, ‘,’)

(e.g., comma is delimiter)– Write with dlmwrite(‘filename’, M, ‘,’)

Page 8: Matlab & LaTeX Tutorial

Manipulations & Calculations

• Transpose (‘), inverse (inv)• Matrix arithmetic: +, -, *, /, ^

– Can use scalar a * ones(m, n) to make matrix of a’s

– repmat(a, m, n) can be faster for big m, n

• Elementwise arithmetic: .*, ./, .^• Relations: >, ==, etc. compare every element

to scalar – M > a returns a matrix the size of M where every

element greater than a is 1 and everything else is 0

• Functions– Vectorized– sin, cos, etc.

Page 9: Matlab & LaTeX Tutorial

Deconstructing Matrices

• Indexing individual entries by row, col: A(1, 1) is upper-left entry

• Ranges: e.g., A(1:10, 3), A(:, 1)• Matrix to vector and vice versa by

column: B = A(:), A(:) = B– Transpose to use row order

• find: Indices of non-zero elements– find(M==a) gives indices of M’s elements

that are equal to a

Page 10: Matlab & LaTeX Tutorial

Vector/Matrix Analysis

• Whole vector• Matrix

– By column•norm: magnitude •max, min: max(max(M)) is maximum

element of matrix M•sum

– By row: transpose, do column analysis

Page 11: Matlab & LaTeX Tutorial

M-Files

• Any text file ending in “.m”• Use path or addpath to tell Matlab where

code is (non-persistent?)• Script: Collection of command line

statements• Function: Take argument(s), return

value(s). First line defines:>> function y = foo(A)>> function [x, y] = foo2(a, M, N)

• Comment: Start line with %

Page 12: Matlab & LaTeX Tutorial

Control Structures

• Expressions, relations (==, >, |, &, functions, etc.)

• if/while expression statements end– Use comma to separate expression from

statements if on same line>> if a == b & isprime(n), M = inv(K); else M = K; end

• for variable = expression statements end>> for i=1:2:100, s = s / 10; end

Page 13: Matlab & LaTeX Tutorial

Plotting

• 2-D vectors: plot(x, y)>> plot(0:0.01:2*pi, sin(0:0.01:2*pi))

• 3-D: plot3(x, y, z)(space curve)• Surfaces

– meshgrid makes surface from axes, mesh plots it>> [X,Y] = meshgrid(-2:.2:2, -2:.2:2);>> Z = X .* exp(-X.^2 - Y.^2);>> mesh(Z)

– surf: Solid version of mesh• Saving figures, plots: print –depsc2 filename or –dpng or –djpgnn, where nn is quality 00-99

Page 14: Matlab & LaTeX Tutorial

Images

• Reading: I = imread(‘filename’);• Accessing/modifying the pixels

– I is m x n if image is grayscale—e.g., I(i, j)– I is m x n x 3 if image is color—e.g., I(i, j, 1)

• Displaying – Just the image: imshow(I);– With overlay:

I = imread(‘gore5.jpg');[r,c,d]=size(I);imshow(I), hold on;plot(c*rand(100,1),r*rand(100,1), 'r*');hold off;

• Writing: imwrite(I, ‘filename’);

Page 15: Matlab & LaTeX Tutorial

LaTeX

• A markup language for typesetting scientific and mathematical documents

• Similar to editing raw HTML, with a lot of special-purpose characters and commands

Page 16: Matlab & LaTeX Tutorial

Documents

• May be easiest to just modify template file provided: cv_math_template.tex

• File anatomy– Header stuff (document type, font size, etc.)—ignore– Title, author– Sections and subsections– Footer stuff—ignore

• Contents– Text– Equations (warning: different ways to get same effect)– Figures, tables: put these kinds of things in your web

page

• Compiling: “pdflatex filename.tex” filename.pdf; preview with Acrobat Reader

Page 17: Matlab & LaTeX Tutorial

Equations• Types

– Inline: $2 + 2 = 4$– Display: \[ a x + by > c \]

• Font types (regular letters are italicized as variables by default)– Roman, bold, italic, calligraphy

• Symbols– Greek letters: \alpha, \beta, etc.– Operators: \infty, \geq, etc.– Functions: \log, \sin, etc.

• Sub- and superscripting: a_{n}, x^{2}– Inverse: \mathbf{A}^{-1}– Transpose: \mathbf{R}^{T}

• Vectors and matrices: Arrays– For example, \left( \begin{array}{c} x \\ y \end{array}

\right)

Page 18: Matlab & LaTeX Tutorial

Function/Macro Equivalent: \newcommand

• Definitions (right after \begin{document})– Simple substitution: \newcommand{\Mean}{\mu}

– Arguments: \newcommand{\MyAdd}[2]{{#1 + #2}

• Invoking – $\Mean$– $\MyAdd{a}{b}$