Intro To MATLAB CS 534 - Fall 2013 Zach Welch
Overview Basics MATLAB data structures Operations Useful functions Image Processing and other useful things for 534 Demo Q&A
Accessing MATLAB MATLAB is available on the Linux and Windows labs On Linux, type matlab into the terminal Can remotely access via ssh
MATLAB Basics Short for MATrix LABoratory High level, interpreted language Great for data heavy applications MATLAB is an interactive, matrix-based system for scientific and engineering numeric computation and visualization. You can solve complex numerical problems in a fraction of the time required with a programming Language such as Fortran or C. ---- MATLAB Primer
MATLAB IDE Current Path Filespace COMMAND WINDOW Where you type commands Workspace List of your current variables Command History List of previous commands
Matrices Building a Matrix Explicitly A = [1 2 3 ;4 5 6;7 8 9] Using a Function B = eye(2,3) zeros,ones,rand 1 2 3 4 5 6 7 8 9 1 0 0 0 1 0 Accessing Elements Matrix indices start at 1,*NOT 0* A(1,2) -> 2
Matrix Operations + Addition - Subtraction * Matrix Multiplication ^ Matrix Power Transpose \ Left Matrix Division (Solves A*x=B) / Right Matrix Division (Solves x*a=b).* Element by Element Multiplication./ Element by Element Division.^ Element by Element Power size get matrix dimensions : access a subset of indices
How Operations Work A = 1 2 3 4 B = 3 1 5 6 A+B = 4 3 8 10 A-B = -2 1-2 -2 A*B = 13 13 29 27 A^2 = 7 10 15 22 A\B = -1 4 2 1.5 solves A*x = B A/B = -.3077.3846.1538.6923 solves x*a = B
A = How Per-Element Operations Work 1 2 3 4 B = 3 1 5 6 C = 1 3 5 7 9 11 13 15 A.*/ B =.333 2.6.666 A.* B = 3 2 15 24 C = 1 5 9 13 3 7 11 15 A.*^ B = 1 2 243 4096
Size B = 1 2 3 4 7 8 size(b,1) = 3 Size gets the dimensions of the matrix. size(matrix, DIM) DIM = 1 -> get row dimension DIM = 2 -> get column dimension size(b,2) = 2 [row,col] =size(b) row => 3 col => 2 FUNCTIONS CAN RETURN MULTIPLE ARGUMENTS IN MATLAB
Colon Operator (:) Extremely useful operator Generates an array of evenly spaced numbers in a user specified range start : increment : stop if increment is left out, assumed to be one
Colon Operator (:) Used to efficiently access submatrices using no numbers -> all elements in dimension A = A(:,1) = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 5 9 13 A(2:end,1) = A(1:2:end, 2:2:end) = 5 9 13 2 4 10 12
Flow Control Logical Operators == is equal to <,>,<=,>= less/greater than ~ not ~= not equal to Instead of using brackets, MATLAB uses end C MATLAB for(int a=0;a<=10;a++){ if( a>3){... } } for (a=0:10) if (a>3) end end...
Flow Control : IF If statements in MATLAB are very similar to if statements in other languages Notice elseif is one word if (boolean) elseif (boolean) else end
Flow Control : WHILE while statements in MATLAB are very similar to while statements in other languages while (boolean)... end
Flow Control : FOR For is most different from other languages Cannot infinite loop in a for loop Uses : operator MATLAB for (a=start:inc:stop) end start : increment : stop C if(start < stop){ if(inc>0){ for(a=start;a<=stop;a+=inc){ } } } else{ if(inc<0){ for(a=start;a>=stop;a+=inc){ } } }
It seems like I can use these loops as I do in C/C++/Java Try to AVOID THIS!
Time Cost Comparison Loop vs. No Loop A = rand(1000,1000);b = rand(1000,1000); for i = 1:size(A,1), for j = 1:size(A,2), C(i,j) = A(i,j) + B(i,j); end end Using loop: Elapsed time is 1.125289 seconds.
Time Cost Comparison(cont.) Loop vs. no loop C = A + B Elapsed time is 0.002346 seconds. Try to take advantage of matrix/vector structure whenever possible
Useful Functions ; - Suppress output who - List current variables help - get information on a function lookfor - keyword search all function help info clear - delete all variables
Writing MATLAB functions Matlab code is saved in.m files 2 kinds of.m files Function.m files Contain a function definition One function per file FILE NAME MUST MATCH FUNCTION NAME Script.m files Contain a list of commands Can be named anything Often used as drivers for functions you have implemented Kind of like main in other languages
Writing MATLAB functions Structure of a MATLAB function function returnval = FunctionName (input1,input2) %Adds two numbers returnval = input1+input2; end Functions Can Return Multiple values function [return1, return2] = FunctionName (input1,input2) return1 = input1+input2; return2= 0; end Make sure you initialize your return variables
Images In MATLAB MATLAB can import/export several image formats: BMP (Microsoft Windows Bitmap) GIF (Graphics Interchange Files) HDF (Hierarchical Data Format) JPEG (Joint Photographic Experts Group) PCX (Paintbrush) PNG (Portable Network Graphics) TIFF (Tagged Image File Format) XWD (X Window Dump) raw-data and other types of image data Data types in MATLAB Double (64-bit double-precision floating point) Single (32-bit single-precision floating point) Int32 (32-bit signed integer) Int16 (16-bit signed integer) Int8 (8-bit signed integer) Uint32 (32-bit unsigned integer) Uint16 (16-bit unsigned integer) Uint8 (8-bit unsigned integer)
Images In MATLAB How to build a matrix (or image)? Intensity Image: o [1, 1] row = 256; col = 256; img = zeros(row, col,3); img(100:105, :,1) = 0.5; img(:, 100:105,2) = 1; imshow(img); Row 1 to 256 o Column 1 to 256 [256, 256]
Image Processing Functions img = imread(imagefilename) - reads in an image file (.jpg,.png,etc) and returns : [width,height] sized matrix if grayscale [width,height,3] sized matrix if rgb imshow(img) - display an image img = im2double(img) - Converts an image (which may be uint8[0-255] or double[0.0-1.0]) to double[0.0-1.0] im2uint8 imwrite(img,filename,filetype) - write out an image Basic structure is: img = imread( input.jpg ); imshow(img); %Do something to the image imshow(img); imwrite(img, output.jpg, jpg );
Questions?
Demos