Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.

Similar presentations


Presentation on theme: "Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine."— Presentation transcript:

1 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.

2 Computer Science in Practice Lecturer: Dani Lischinski (danix@cs.huji.ac.il)danix@cs.huji.ac.il TA: Amit Gruber Course email : csip@cs.huji.ac.il Personal email : amitg@cs.huji.ac.il Reception hour: Sunday 16:00-17:00csip@cs.huji.ac.ilamitg@cs.huji.ac.il Web site: www.cs.huji.ac.il/~csip

3 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

4 Matlab/Octave Basics http://www.octave.org/doc/index.html http://www.mathworks.com/access/helpd esk/help/techdoc/matlab_prog/exampleind ex.html (M-file programming)http://www.mathworks.com/access/helpd esk/help/techdoc/matlab_prog/exampleind ex.html Use help

5 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 line function [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 elements it 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)

6 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

7 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

8 Code optimization – vectorizing loops Convert for / while loops to equivalent vector or matrix operations 1D indexing >> for x = 1:k ff(x) = 5*sin((x-1)/(2*pi)); end >> x = 0:k-1 >> ff = 5*sin(x/(2*pi));

9 Code optimization – vectorizing loops 2D indexing meshgrid – 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 >> [C, R] = meshgrid(1:c, 1:r) >> b = R.^2 + C.^2;

10 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);

11 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);

12 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’

13 A few more important commands (look in the help) find repmat reshape clear save plot, subplot disp

14 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


Download ppt "Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine."

Similar presentations


Ads by Google