CSE 455: Computer Vision: Matlab 101 Getting Started With MATLAB
CSE 455: Computer Vision: Matlab 101 Getting Started With MATLAB
MATLAB 101
Getting Started with MATLAB
Why?
• All assignments are going to be in MATLAB
Workspace
Files in Command
current Prompt
directory
Command
History
Getting Started
• Common operators: +,-,/,*
• Variables:
– Assigned using =
– There is no need to explicitly define the type of
variable
Control of Flow
• if statements and loops
If <logical expression>
<statements>
end
• Transposing a matrix: A’
Manipulating Matrices
• Some very useful operations:
– B = reshape(A,m,n) : Takes a matrix A with m*n
elements and reshapes it into a matrix with m
rows and n columns
1 4 7 10
2 5 8 11
3 6 9 12
Reshape(A,2,6)
1 3 5 7 9 11
2 4 6 8 10 12
Manipulating Matrices
• Some very useful operations:
– Concatenating matrices:
A= 1 2 3 [A;B] 1 2 3
4 5 6
B= 4 5 6
1 4
1 4 [A B]
2 5
A= 2 B= 5
3 6
3 6
Manipulating Matrices
• Some very useful operations:
– Concatenating matrices:
A= 1 2 3 cat(1,A,B) 1 2 3
4 5 6
B= 4 5 6
1 4
1 4 cat(2,A,B)
2 5
A= 2 B= 5
3 6
3 6
1 2 1 2 1 2
3 4 3 4 3 4
1 2 repmat(A,2,3)
1 2 1 2 1 2
3 4
3 4 3 4 3 4
Manipulating Matrices
• find
>> inds = find(A>0);
• sum
>> sum(A,1); % sum A along first dimension
OR
• doc <function_name>
Matlab Scripts: m-files
• Save a sequence of MATLAB commands as a
script
Take weighted sum of values in the box, weights specified by the filter
1*1 + 1*0 + 1*1 + 1*0 + 2*5 + 1*0 + 1*0 + 1*3 + 1*0 = 15
Filter
1 1 1
1 2 1
1 1 1
Image Filtering Example
Input Image Filtered Image
1 0 1 2 7 8
0 5 0 3 6 9
0 3 0 0 6 1
0 3 0 6 7 8
6 6 5 5 4 4
Filter
1 1 1
1 2 1
1 1 1
MATLAB Demo
MATLAB Tips
• MATLAB is slow
MATLAB Tips
• MATLAB can be slow
• surf/mesh