140 likes | 254 Views
Computer Science in Practice. This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine learning, computer vision and graphics. We will mainly use tools of linear algebra. Computer Science in Practice.
E N D
Computer Science in Practice • This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine learning, computer vision and graphics. • We will mainly use tools of linear algebra.
Computer Science in Practice • Lecturer: Dani Lischinski (danix@cs.huji.ac.il) • TA: Amit Gruber Course email : csip@cs.huji.ac.ilPersonal email : amitg@cs.huji.ac.ilReception hour: Sunday 16:00-17:00 • Web site: www.cs.huji.ac.il/~csip
Requirements of the course • Exercises (every 2-3 weeks) • Exercises are submitted in pairs • Programming in Matlab or Octave • Experimental validation • Theoretical questions • A final exam
Matlab/Octave Basics • http://www.octave.org/doc/index.html • http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/exampleindex.html (M-file programming) • Use help
M-File Programming • M-files can be scripts or functions that accept input arguments and produce one or more outputs.Components of m files: • Function definition linefunction [out1 out2] = name(in1, in2, in3) • H1 line - a single comment line that follows the function definition line. % SQUARESUM compute the sum of the square of the matrix elementsit is the first text that appears when user write>> help function_name>> lookfor keyword display all functions where the keyword appeared in H1 line • Help Text - text block following the H1 line without any blank line in between the two • Function body – the Matlab code • Comments – lines starting with % (note – add short and clear comments to your code)
Operators • Arithmetic operators (numeric computations) • matrix arithmetic (linear algebra A*B) • array arithmetic (element by element A.*B) +, -, ./, .^,:.. • Relational operators (compare) • Compare corresponding elements of arrays of equal dimensions (<, >,<=, >=, ==, ~=) or an array to scalar • Logical operators can operate both on logical and numeric data (and: &, or: |, not: ~)true: logical 1 or non-zero numeric quantity false: logical 0 or numerical 0 logical functions : xor, any, all
Flow control • if, else, elseif, end • switch, case, otherwise, end • return • try,..catch • for i=start:increment:end, end • while, end • break (used with for or while) • continue (used with for or while) Try not to use
>> x = 0:k-1 >> ff = 5*sin(x/(2*pi)); Code optimization – vectorizing loops • Convert for / while loops to equivalent vector or matrix operations1D indexing >> for x = 1:k ff(x) = 5*sin((x-1)/(2*pi)); end
Code optimization – vectorizing loops • 2D indexingmeshgrid – convert rows vectors to arrays C and R that can be used for evaluating function with two variables >> for r = 1:10 >> for c = 1:10 >> b(r,c) = r^2+ c^2 >> end >> end >> [C, R] = meshgrid(1:c, 1:r) >> b = R.^2 + C.^2;
Code optimization – vectorizing loops • function B = repmat(A,M,N) • [m,n] = size(A); • mind = (1:m)’; • nind = (1:n)’; • mind = mind(:,ones(1,M)); • nind = nind(:,ones(1,N)); • B = A(mind,nind);
Code Optimization – Preallocating arrays • Simple way to improve code execution is to pre-allocate the size of the arrays in the program.The preallocation also help reduce memory fragmentation when working with large matrixes >> f = zeros(1024);
Cell arrays and Structures • Cell array is multidimensional array whose elements are copies of other arrays>> c = {‘gauss’,[1 0;0 1], 3}>> c{1} ans = gauss • Structures are similar to cell arrays (allow grouping of a collection of dissimilar data) but they addressed by fields rather than by numbers>> params.nimgs = 100; >> params.jump = 2; >> params.baseStr = ‘testImg’
A few more important commands (look in the help) • find • repmat • reshape • clear • save • plot, subplot • disp
Exercise 1 • Find the first eigenvector and eigenvalue of a matrix using the power method. • Targets: • Getting familiar with Octave • Understanding the Power Mehtod • Designing and performing experiments