Download presentation
Presentation is loading. Please wait.
Published byLeo Cunningham Modified over 9 years ago
1
6.S093: Visual Recognition through Machine Learning Competition MATLAB tutorial
2
Getting Started To get MATLAB student version, go to http://ist.mit.edu/matlab/all/student (or google MIT MATLAB) http://ist.mit.edu/matlab/all/student On Athena (athena.dialup.mit.edu), » add matlab » matlab &
3
MATLAB Very easy to learn. Takes some careful attention for a fast code. Lots of great documentation – Confused? » help command Go to mathworks documentation website http://www.mathworks.com/help/matlab/ http://www.mathworks.com/help/matlab/
4
First Exercise Print [Hello World] Multiple solutions… – Easy way » disp ‘Hello World’; – Traditional way (formatting is supported) » fprintf(‘Hello World\n’);
5
Variable types and assignment By default, – Number » tmp1 = 3; % 8-bytes double – Char » tmp2 = ‘a’; % 2-bytes char You need to specify when it is not double. » tmp3 = single(3); % 4-bytes float » tmp4 = int16(3); % 2-bytes int16 » tmp5 = false; % 1-byte logical » whos % lists defined variables
6
Matrix MATLAB uses Matrix (rather than array). Matrix uses (row, column) » a = [1 2; 3 4]; » whos Name Size Bytes Class Attributes a 2x2 32 double » a(2, 1:2) ans = 3 4
7
Vectorization How do you compute a dot product? a = 1:1e8; b = cos(1:1e8); % a vector of cos of 1:1e8 c = 0; for i=1:length(a) % for-loop from 1 through length(a) c = c + a(i) * b(i); end
8
Vectorization How do you compute a dot product? MATLAB is optimized for matrix/vector operations – First solution takes: 2.02s – Second solution takes: 0.16s a = 1:1e8; b = cos(1:1e8); % a vector of cos of 1:1e8 c = 0; for i=1:length(a) % for-loop from 1 through length(a) c = c + a(i) * b(i); end d = a*b’; % matrix multiplication
9
Image Functions Imagesc imread rgb2gray edge figure imresize imrotate imfilter
10
Other useful functions Operation: repmat, bsxfun, find Data structure: cell array, structure Matrix operation: inv, eig, svd Statistical function: hist, rand, randn Function: nargin, nargout Debug: keyboard, dbstop if error, dbstack Performance: tic/toc, profile
11
Image comparison Compare these 3 images using the average pixel value difference – Step1: resize image (imresize) – Step2: subtract im1 from im2 and im3 – Step3: take an average of absolute difference http://viscomp.csail.mit.edu/resource/matlab /image_compare.m http://viscomp.csail.mit.edu/resource/matlab /image_compare.m
12
Exercise: Image blurring Optimize the provided blurring code Our blurring algorithm: for each pixel, assign the average value of neighborhood pixels. http://viscomp.csail.mit.edu/resource/matlab /image_blur.m http://viscomp.csail.mit.edu/resource/matlab /image_blur.m
13
Summary Great documentation Vectorization! = AVOID for-loop! Matrix is (row, column) rather than (x, y). – Image has [height] x [width]! [1..n] rather than [0..n-1] LOTS of optimized functions available
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.