Introduction To MATLAB (Basics) : Reference From: Azernikov Sergei Mesergei@tx - Technion.ac - Il
Introduction To MATLAB (Basics) : Reference From: Azernikov Sergei Mesergei@tx - Technion.ac - Il
MATLAB Basics
Where to get help? 1) In MATLABs prompt type: help, lookfor,helpwin, helpdesk, demos. 2) On the Web: http://www.mathworks.com/support
2
MATLABs Workspace
who,whos - current variables in workspace save - save workspace variables to *.mat file load - load variables from *.mat file clear all - clear workspace variables
Matrices in MATLAB
Matrix is a main MATLABs data type How to build a matrix? A = [ 1 2 3; 4 5 6; 7 8 9 ]; Creates matrix A with size 3x3. Special matrices : zeros(n,m), ones(n,m),eye (n,m)
4
Logical Conditions
== , < , > , (not equal)~= ,(not)~ find(condition) - Returns indexes of As
elements that satisfies the condition.
Logical Conditions(cont.)
Example:
>> A = [1 2; 3 4], I = find(A<4) A= 1 3 2 4
I= 1 2 3
7
Flow Control
MATLAB has five flow control constructs:
if statements switch statements for loops while loops break statements
8
if
IF statement condition. The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END
9
if(cont.)
Example:
switch
SWITCH - Switch among several cases based on expression. The general form of the SWITCH statement is:
SWITCH switch_expr CASE case_expr, statement, ..., statement CASE {case_expr1, case_expr2, case_expr3,...} statement, ..., statement ... OTHERWISE, statement, ..., statement END
11
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).
12
for
FOR Repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., END
13
for (cont.)
Example: FOR I = 1:N, FOR J = 1:N, A(I,J) = 1/(I+J-1); END END
14
while
WHILE Repeat statements an indefinite number of times. The general form of a WHILE statement is: WHILE expression statements END
15
while (cont.)
Example: E = 0*A; F = E + eye(size(E)); N = 1; while norm(E+F-E,1) > 0, E = E + F; F = A*F/N; N = N + 1; end
16
Functions in MATLAB
FUNCTION Add new function. New functions may be added to MATLAB' s vocabulary if they are expressed in terms of other existing functions.
18
23
Indexed Images
[x,map] = imread(' trees.tif' ); imshow(x,map);
24
Intensity Images
image = ind2gray(x,map); imshow(image);
25
Binary Images
imshow(edge(image));
26
RGB Images
27
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
28
Displaying images
figure, imshow(I)
Points to Note
All arithmetic operations performed on matrices may be performed on images After processing, an image matrix can be written to an output image file with the imwrite function
- imwrite(I,map,filename,fmt)
Without the map argument, the image data is supposed to be grayscale or RGB. The format fmt needs to support the particular type of image
30
Image Conversion
gray2ind - intensity image to index image im2bw - image to binary im2double - image to double precisb dex im
31
% read a TIFF image % display it as indexed image % convert it to grayscale % use gray colormap % scale data to use full colormap % for values between 0 and 1 % make displayed aspect ratio %proportional % to image dimensions %array
% between min and max values in I2 colormap(' gray' ) colorbar pixval truesize truesize(2*size(I2)) I3=imresize(I2,0.5,' bil' ); I3=imrotate(I2,45,' bil' ); I3=double(I2); % math operations imagesc(I3.^2) imagesc(log(I3)) % display squared image (pixel-wise) % display log of image
33
% turn on color bar % display pixel values interactively % display at resolution of one %screen pixel % per image pixel % display at resolution of two %screen pixels % per image pixel % resize by 50% using bilinear % interpolation % rotate 45 degrees and crop to % original size % convert from uint8 to double, to %allow
MATLAB Resources on the Internet http://www.mathworks.com/products/demos/# http://www.math.siu.edu/MATLAB/tutorials.html http://math.ucsd.edu/~driver/21d -s99/MATLAB-primer.html http://www-cse.ucsd.edu/~sjb/classes/MATLAB/MATLAB.intro.html http://www.mit.edu/~pwb/cssm/ http://www.mathworks.com Interesting and very complete tutorials in: http://www.mathworks.com/academia/student_center/tutorials/la unchpad.html http://www.mathworks.com/matlabcentral/fileexchange
34
MATLAB tutorial http://www.math.mtu.edu/~msgocken/intro/intro.html http://amath.colorado.edu/scico/tutorials/matlab/ MATLAB helpdesk http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml MATLAB Primer ftp://ftp.eng.auburn.edu/pub/sjreeves/matlab_primer_40.pdf
35