Download presentation
Presentation is loading. Please wait.
Published byAlexis Hodges Modified over 9 years ago
1
Matlab & LaTeX Tutorial Course web page: vision.cis.udel.edu/cv February 14, 2003 Lecture 2
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
3
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.
4
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
5
How to Get Help In Matlab –Type “ help ” to get a listing of topics –“ help ” 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
6
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
7
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, ‘,’)
8
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.
9
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
10
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
11
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 %
12
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
13
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
14
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’);
15
LaTeX A markup language for typesetting scientific and mathematical documents Similar to editing raw HTML, with a lot of special-purpose characters and commands
16
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
17
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)
18
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}$
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.