Getting Started With Matlab: Cs534 Ta: Matt Mcdaniel Alrecenk@Cs - Wisc.Edu Sep 17, 2012 - Fall 2011
Getting Started With Matlab: Cs534 Ta: Matt Mcdaniel Alrecenk@Cs - Wisc.Edu Sep 17, 2012 - Fall 2011
Outline
Introduction to MATLAB
Basics & Examples
What is MATLAB?
MATLAB = Matrix Laboratory 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 Help
Search by keywords
Entering Matrices
By an explicit list of elements
A=[1,2,3; 4 5 6; 7 8, 9];
Be sure about whether you are using matrix operators or element-wise operators!
Logical Operators
==, <, >, (not equal) ~=, (not) ~ find(condition) Returns indexes of As elements that satisfy the condition
r =
1 2 2
c = 2 2 3
CODE
Flow Control
MATLAB has five flow control constructs:
if statement switch statement for loop while loop break statement
if
IF statement condition
The general form of the IF statement is
IF expression
statements
ELSEIF expression
statements
ELSE
statements
END
CODE
switch
SWITCH Switch among several cases based on expression The general form of SWITCH statement is:
SWITCH switch_expr
CASE case_expr,
statement, , statement
OTHERWISE
statement, , statement
END
switch (cont.)
Note:
Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed Unlike C, the SWITCH statement does not fall through (so BREAKs are unnecessary)
CODE
for
FOR repeats statements a specific number of times The general form of a FOR statement is:
FOR variable=expr
statements
END
CODE
while
WHILE repeats statements an indefinite number of times The general form of a WHILE statement is:
WHILE expression
statements
END
CODE
C=A+B
Debug Techniques
Debug is essential. Easy and flexible access during debug. DEMO
Outline
Introduction to MATLAB
Basics & Examples
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
Images in MATLAB
Binary images : {0,1} Intensity images : [0,1] or uint8, double etc.
RGB images : m n 3 Multidimensional images: m n p (p is the number of layers)
row = 256; col = 256; img = zeros(row, col); img(100:105, :) = 0.5; img(:, 100:105) = 1; figure; imshow(img);
Image Display
image - create and display image object imagesc - scale and display as image imshow - display image colorbar - display colorbar getimage - get image data from axes truesize - adjust display size of image zoom - zoom in and zoom out of 2D plot
Image Conversion
gray2ind - intensity image to index image im2bw - image to binary im2double - image to double precision im2uint8 - image to 8-bit unsigned integers im2uint16 - image to 16-bit unsigned integers ind2gray - indexed image to intensity image mat2gray - matrix to intensity image rgb2gray - RGB image to grayscale rgb2ind - RGB image to indexed image
Image Operations
RGB image to gray image Image resize Image crop Image rotate Image histogram Image histogram equalization Image DCT/IDCT Convolution
- CODE
Outline
Introduction to MATLAB
Basics & Examples
y = imfilter(x,h) DEMO
Performance Issues
The idea: MATLAB is
very fast on vector and matrix operations Correspondingly slow with loops
THE END
Thanks for your attention! Questions?