Matlab Matrices

advertisement
1
NET 222: COMMUNICATIONS AND
NETWORKS FUNDAMENTALS
(PRACTICAL PART)
Networks and
Communication
Department
Lab 1 : Introduction to MatlaB
Lecture Contents
2





Matlab introduction
Why Matlab
Matlab Software install and interface
Matlab Variables
Matlab Matrices
Networks and Communication Department
Matlab introduction
3


MatLab : Matrix Laboratory
Numerical Computations with matrices
 Every

number can be represented as matrix
Why Matlab?
 User
Friendly (GUI)
 Easy to work with
 Powerful tools for complex mathematics
 It was originally designed for solving linear algebra type
problems using matrices
Networks and Communication Department
Why Matlab?
Graphing
 A Comprehensive array of plotting options
available from 2 to 4 dimensions
 Full control of formatting, axes, and other visual
representational elements
Why Matlab?
Multi-platform, Multi Format data importing
 Data can be loaded into Matlab from almost
any format and platform
 Analog/Digital Data files
PC
100101010
UNIX
Subject 1 143
Subject 2 982
Subject 3 87 …
Matlab Software install
6
Matlab Interface
7
Title bar
Menu bar
help
Current working Directory
Current Directory
contents
command window
File details
Workspace(variable
list)
Function Catalog
Networks and Communication Department
Matlab Interface
8
Title bar
Menu bar
help
Current working Directory
The MATLAB environment is command oriented
Current Directory
contents
command window
File details
Workspace(variable
list)
Function Catalog
Networks and Communication Department
Matlab Interface
9
Title bar
help
Menu bar
Current working Directory
View folders and m-files
Current Directory
contents
command window
type commands
Command History
view past commands save a
whole session using diary
File details
Workspace(variable
list)
Function Catalog
Networks and Communication Department
To appear Command History, select layout
10
11
All MATLAB windows are docked in the default desktop,
which means that they are tiled on the main MATLAB
window.
Docked
window
Undocked
window
Networks and Communication Department
Command window
12
MATLAB expressions and
statements are evaluated as
you type them in the
Command Window, and
results of the computation
are displayed there too.
Networks and Communication Department
Variables

No need for types. i.e.,
int a;
double b;
float c;

All variables are created with double precision unless specified
and they are matrices.
Example:
>>x=5;
>>x1=2;

After these statements, the variables are 1x1 matrices with
double precision
Creating Variables
 Names



Can be any string of upper and lower case letters along with numbers
and underscores but it must begin with a letter
Reserved names are IF, WHILE, ELSE, END, SUM, etc.
Names are case sensitive
 Value

This is the data the is associated to the variable; the data is accessed
by using the name.
Example of Value : Singletons
 To
assign a value to a variable use the equal symbol
‘=‘
>> A = 32
 To
find out the value of a variable simply type the
name

The value of two variables can be added together, and
the result displayed…
>> A = 10
>> A + A

or the result can be stored in another variable
>> A = 10
>> B = A + A
Matlab Assignment &Operators
17







Assignment =
Addition +
Subtraction Multiplication * or.*
Division / or ./
Power ^ or .^
' transpose
a = b (assign b to a).
a+b
a -b
a*b or a.*b
a/b or a./b
a^b or a.^b
a'
Networks and Communication Department


Variables are assigned numerical values by typing the
expression directly.
for example a = 1+2
yields: a = 3
The answer will not be displayed when a semicolon is put at the
end of an expression, for example
a = 1+2; (no answer)
 For example, since “a” was defined previously, the following
expression is valid b = 2*a
yields: b = 6
Matrices

A MATLAB matrix is a rectangular array of numbers

Scalars and vectors are special cases of matrices

MATLAB allows you to work with a whole array at a time

Scalar: A matrix with one row and one column.
A = [3.5]

Vector: A matrix with one row (a row vector) or one column, ( a column
vector). Use square brackets [] to contain the numbers
Note that in MATLAB the first index of a vector or
matrix starts at 1, not 0 as is common with other
programming languages.
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 =
123
456
789

a vector
x = [1 2 5 1]
x =
1

2
transpose
5
1
y = x’
y =
1
2
5
1

a matrix
x = [1 2 3; 5 1 4; 3 2 -1]
x =
1
5
3
2
1
2
3
4
-1
Long Array, Matrix
t =1:10
t =
k
1
=2:-0.5:-1
2
3
4
1.5
1
0.5
5
6
7
8
k =
2
B
= [1:4; 5:8]
B =
1
5
2
6
3
7
4
8
0
-0.5
-1
9
10
Extracting a Sub-Matrix
24



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:
where r1 and r2 specify the beginning and ending
rows and c1 and c2 specify the beginning and ending
columns to be extracted to make the new matrix.
Networks and Communication Department
Matlab Matrices
25
• A column vector can be
extracted from a matrix.
As an example we
create a matrix below:
• Here we extract column
2 of the matrix and
make a column vector:
Networks and Communication Department
Matlab Matrices
26
A row vector can be
extracted from a matrix.
As an example we
create a matrix below:
Here we extract row 2 of the
matrix and make a row
vector. Note that the 2:2
specifies the second row
and the 1:3 specifies which
columns of the row.
Networks and Communication Department
Matrix Index
The matrix indices begin from 1 (not 0 (as in C))
The matrix indices must be positive integer


Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Concatenation of Matrices

x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1
2
4
5
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Some matrix functions in Matlab
29







X = ones(r,c)
X = zeros(r,c)
A = diag(x)
[r,c] = size(A)
+-*/
.+ .- .* ./
v = sum(A)
% Creates matrix full with ones.
% Creates matrix full with zeros.
% Creates squared matrix with
vector x in diagonal.
% Return dimensions of matrix A.
% Standard operations.
% Wise addition, substraction ,…
% Vector with sum of columns.
Networks and Communication Department
Example of Function
x = zeros(1,3)
x =
0
0
0
x = ones(1,3)
x =
1
1
1
x = rand(1,3)
x =
0.9501 0.2311 0.6068
Example
>>x=(0:0.1:1)'
>>y=sin(x)
>>[x y]
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
‘ complex conjugate transpose
Matrices Operations
Given A and B:
Addition
Subtraction
Product
Transpose
Operators (Element by Element)
.* element-by-element multiplication
./ element-by-element division
.^ element-by-element power
The use of “.” – “Element” Operation
A = [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
3 2 -1
x = A(1,:)
x=
c=x./y
d = x .^2
b=
c=
0.33 0.5 -3
d=
y = A(3 ,:)
y=
1 2 3
b = x .* y
3 8 -3
3 4 -1
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
1
4
9
Functions
36


MATLAB has many built-in functions.
Some math functions are:
Networks and Communication Department
Matlab Special Variables






ans: default variable name
pi: ratio of circle circumference to its diameter, π =
3.1415926...
eps: smallest amount by which two numbers can differ
inf or Inf : infinity
nan or NaN : not-a-number, e.g. 0/0
date: current date in a character string format, such as
19-Mar-1998.
Commands involving variables:








who: lists the names of defined variables
whos: lists the names and sizes of defined variables
clear: clears all variables, resets default values of special variables
clear var: clears variable var
clc: clears the command window, homes the cursor (moves the prompt
to the top line), but does not affect variables.
clf: clears the current figure and thus clears the graph window.
more on: enables paging of the output in the command window.
more off: disables paging of the output in the command window.
Note
39


The '%' sign indicates that there is a comment in
that line and Matlab does not do anything with it. It
is as if it were inexistent, and it exists only for
explanatory purposes.
“%” is the neglect sign for Matlab (equaivalent of
“//” in C).
Networks and Communication Department
Useful Commands

The two commands used most by Matlab
users are
>>help functionname
>>lookfor keyword
41
The End
Any Questions ?
Networks and Communication Department
Download