Introduction To MATLAB Lec. Ass: Howida Youssry Pattern Recognition (CS451)
Introduction To MATLAB Lec. Ass: Howida Youssry Pattern Recognition (CS451)
Introduction To MATLAB Lec. Ass: Howida Youssry Pattern Recognition (CS451)
1
MATLAB
» a=5;
» b=a/2
b=
2.5000
»
MATLAB Variable Names
Variable names ARE case sensitive
- (unary) + (unary)
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
Other MATLAB symbols
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
MATLAB Matrices
MATLAB treats all variables as matrices. For our purposes a
matrix can be thought of as an array, in fact, that is how it is
stored.
Scalars are matrices with only one row AND one column
MATLAB Matrices
A matrix with only one row AND one column is a scalar. A
scalar can be created in MATLAB as follows:
» a_value=23
a_value =
23
Plot function
>> a=[ 3 4 5;3 6 7;6 7 8]
a=
3 4 5
3 6 7
6 7 8
>> plot(a)
Reading Data from files
MATLAB supports reading an entire file and creating a matrix
of the data with one statement.
MATLAB will also plot a vector vs. its own index. The index
will be treated as the abscissa vector. Given a vector “time”
and a vector “dist” we could say:
>> plot (time, dist) % plotting versus time
>> plot (dist) % plotting versus index
Plotting with MATLAB
There are commands in MATLAB to "annotate" a plot to put
on axis labels, titles, and legends. For example:
>> % To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')
if expression1 % is true
% execute these commands
elseif expression2 % is true
% execute these commands
else % the default
% execute these commands
end
MATLAB Repetition Structures
A for loop in MATLAB for x = array
for ind = 1:100
b(ind)=sin(ind/10)
end
Alternative:
x=0.1:0.1:10; b=sin(x); - Most of the loops can be avoided!!!