1 / 27

Intro To MATLAB

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

nysa
Télécharger la présentation

Intro To MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro To MATLAB CS 534 - Fall 2013 Zach Welch

  2. Overview • Basics • MATLAB data structures • Operations • Useful functions • Image Processing and other useful things for 534 • Demo • Q&A

  3. Accessing MATLAB • MATLAB is available on the Linux and Windows labs • On Linux, type “matlab” into the terminal • Can remotely access via ssh

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

  5. MATLAB IDE Current Path COMMAND WINDOW Where you type commands Workspace List of your current variables Filespace Command History List of previous commands

  6. 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 • Accessing Elements • Matrix indices start at 1,*NOT 0* • A(1,2) -> 2 1 2 3 4 5 6 7 8 9 1 0 0 0 1 0

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

  8. How Operations Work -.3077 .3846 .1538 .6923 -1 4 2 1.5 7 10 15 22 -2 1 -2 -2 13 13 29 27 1 2 3 4 3 1 5 6 4 3 8 10 A/B = A^2 = A*B = A+B = A-B = A\B = A = B = solves A*x = B solves x*A = B

  9. How Per-Element Operations Work C’ = C = 1 3 5 7 9 11 13 15 1 2 243 4096 3 2 15 24 .333 2 .6 .666 3 1 5 6 1 2 3 4 1 5 9 13 3 7 11 15 A .*/ B = A .*^ B = A .* B = B = A =

  10. Size Size gets the dimensions of the matrix. size(Matrix, DIM) DIM = 1 -> get row dimension DIM = 2 -> get column dimension 1 2 3 4 7 8 B = size(B,1) = 3 size(B,2) = 2 FUNCTIONS CAN RETURN MULTIPLE ARGUMENTS IN MATLAB [row,col] =size(B) row => 3 col => 2

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

  12. Colon Operator (:) • Used to efficiently access submatrices • using no numbers -> all elements in dimension A(1:2:end, 2:2:end) = A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 A(:,1) = 1 5 9 13 2 4 10 12 A(2:end,1) = 5 9 13

  13. Flow Control • Logical Operators • ==is equal to • <,>,<=,>= less/greater than • ~ not • ~= not equal to • Instead of using brackets, MATLAB uses “end” C MATLAB for (a=0:10) if (a>3) ... … end end for(int a=0;a<=10;a++){ if( a>3){ ... … } }

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

  15. Flow Control : WHILE • while statements in MATLAB are very similar to while statements in other languages while (boolean) ... end

  16. Flow Control : FOR • For is most different from other languages • Cannot infinite loop in a for loop • Uses : operator • start : increment : stop MATLAB 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){ … } } } for (a=start:inc:stop) … end

  17. It seems like I can use these loops as I do in C/C++/Java… Try to AVOID THIS!

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

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

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

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

  22. Writing MATLAB functions • Structure of a MATLAB function • Functions Can Return Multiple values • Make sure you initialize your return variables function returnVal = FunctionName (input1,input2) %Adds two numbers returnVal = input1+input2; end function [return1, return2] = FunctionName (input1,input2) return1 = input1+input2; return2= 0; end

  23. 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)

  24. Images In MATLAB [1, 1] • How to build a matrix • (or image)? • Intensity Image: • row = 256; • col = 256; • img = zeros(row, col,3); • img(100:105, :,1) = 0.5; • img(:, 100:105,2) = 1; • imshow(img); o Row 1 to 256 o Column 1 to 256 [256, 256]

  25. 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’);

  26. Questions?

  27. Demos

More Related