220 likes | 562 Views
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
E N D
Matlab & LaTeXTutorial 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 • Read about 3-D geometry in Chapter 2-2.1 of Forsyth & Ponce for Monday’s lecture
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.
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
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
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
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, ‘,’)
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.
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
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
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 %
Control Structures • Expressions, relations (==, >, |, &, functions, etc.) • if/whileexpressionstatementsend • Use comma to separate expression from statements if on same line >> if a == b & isprime(n), M = inv(K); else M = K; end • forvariable=expressionstatementsend >> for i=1:2:100, s = s / 10; end
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 –depsc2filename or –dpng or –djpgnn, where nn is quality 00-99
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’);
LaTeX • A markup language for typesetting scientific and mathematical documents • Similar to editing raw HTML, with a lot of special-purpose characters and commands
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
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)
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}$