Tutorial 1
Tutorial 1
MATLAB
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
MATLAB Matrices
A matrix with only one row is called a row vector. A row
vector can be created in MATLAB as follows (note the
commas):
rowvec =
12 14 63
MATLAB Matrices
A matrix with only one column is called a column vector. A
column vector can be created in MATLAB as follows (note
the semicolons):
colvec =
13
45
-2
MATLAB Matrices
A matrix can be created in MATLAB as follows (note the
commas AND semicolons):
» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1 2 3
4 5 6
7 8 9
Extracting a Sub-Matrix
A portion of a matrix can be extracted and stored in a smaller
matrix by specifying the names of both matrices and the rows
and columns to extract. The syntax is:
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
» matrix=[1,2,3;4,5,6;7,8,9] » col_two=matrix( : , 2)
matrix = col_two =
1 2 3
4 5 6 2
7 8 9 5
8
MATLAB Matrices
A row vector can be Here we extract row 2 of
extracted from a matrix. the matrix and make a row
As an example we create a vector. Note that the 2:2
matrix below: specifies the second row
and the 1:3 specifies which
columns of the row.
» matrix=[1,2,3;4,5,6;7,8,9]
» rowvec=matrix(2 : 2 , 1 : 3)
matrix =
rowvec =
1 2 3
4 5 6
4 5 6
7 8 9
Reading Data from files
MATLAB supports reading an entire file and creating a matrix
of the data with one statement.
>> load mydata.dat; % loads file into matrix.
% The matrix may be a scalar, a vector, or a
% matrix with multiple rows and columns. The
% matrix will be named mydata.
>> size (mydata) % size will return the number
% of rows and number of
% columns in the matrix
>> length (myvector) % length will return the total
% no. of elements in myvector
Plotting with MATLAB
MATLAB will plot one vector vs. another. The first one will
be treated as the abscissa (or x) vector and the second as the
ordinate (or y) vector. The vectors have to be the same
length.
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!!!