Intro To MATLAB -...
Transcript of 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 ● 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
COMMAND WINDOW Where you type commands
Workspace List of your current variables
Command History List of previous commands
Current Path
Filespace
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
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
3 1 5 6 B =
1 2 3 4 A =
4 3 8 10 A+B =
-2 1 -2 -2 A-B =
13 13 29 27 A*B =
7 10 15 22 A^2 =
solves A*x = B -.3077 .3846 .1538 .6923 A/B =
-1 4 2 1.5 A\B = solves x*A = B
How Per-Element Operations Work
3 2 15 24 A .* B =
.333 2
.6 .666 A .*/ B =
1 2 243 4096 A .*^ B =
3 1 5 6 B =
1 2 3 4 A =
1 3 5 7 9 11 13 15
C =
1 5 9 13 3 7 11 15
C’ =
Size 1 2 3 4 7 8
B =
size(B,1) = 3
size(B,2) = 2
[row,col] =size(B) row => 3 col => 2
FUNCTIONS CAN RETURN MULTIPLE ARGUMENTS IN MATLAB
Size gets the dimensions of the matrix. size(Matrix, DIM) DIM = 1 -> get row dimension DIM = 2 -> get column dimension
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 1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16
A =
1 5 9
13
A(:,1) =
5 9
13
A(2:end,1) =
2 4 10 12
A(1:2:end, 2:2:end) =
● Logical Operators ○ == is equal to ○ <,>,<=,>= less/greater than ○ ~ not ○ ~= not equal to
● Instead of using brackets, MATLAB uses “end”
Flow Control
for(int a=0;a<=10;a++){ if( a>3){
... …
} }
C
for (a=0:10) if (a>3)
... …
end end
MATLAB
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
● For is most different from other languages ● Cannot infinite loop in a for loop ● Uses : operator
○ start : increment : stop
Flow Control : FOR
if(start < stop){ if(inc>0){ for(a=start;a<=stop;a+=inc){ … } } } else{ if(inc<0){ for(a=start;a>=stop;a+=inc){ … } } }
C for (a=start:inc:stop) … end
MATLAB
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
● 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
• 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
Column 1 to 256
Row
1 to 256
o
[1, 1]
o
[256, 256]
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);
Images In MATLAB
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