Sei sulla pagina 1di 11

Part II:Matrix Operation &

Basic Plotting
EE 290 Class Notes

Matrix Operation (1)


Array/Matrix can be generated:
By direct assignment
Using colon operators
Using Matrix generation functions:
eye(m,n)
eye(m,n)
eye(n)
eye(n)
zeros(m,n)
zeros(m,n)
ones(m,n)
ones(m,n)
diag(A)
diag(A)
rand(m,n)
rand(m,n)

Returns
Returns an
an m-by-n
m-by-n matrix
matrix with
with 11 on
on the
the main
main diagonal
diagonal
Returns
Returns an
an n-by-n
n-by-n square
square identity
identity matrix
matrix
Returns
Returns an
an m-by-n
m-by-n matrix
matrix of
of zeros
zeros
Returns
Returns an
an m-by-n
m-by-n matrix
matrix of
of ones
ones
Extracts
Extracts the
the diagonal
diagonal of
of matrix
matrix A
A
Returns
Returns an
an m-by-n
m-by-n matrix
matrix of
of random
random numbers
numbers

Matrix Operation (2)


Matrix Indexing
The element of row i and column j of the
matrix A is denoted by A(i,j).
Colon operator : can be used to refer to the
entire row/column of the matrix
A(i,:) all elements of row i
A(:,j) all elements of column j

Use array as matrix index

Submatrix

Matrix Operation (3)


Examples:
Example
Example 1:
1:
>>[height,
>>[height, width]=size(A)
width]=size(A)

>>B=A(1:height/2,
>>B=A(1:height/2, 1:width/2);
1:width/2);
Q:
Q: What
What is
is the
the relation
relation b/w
b/w A
A
and
and B?
B?

Q:
Q: What
What is
is the
the relation
relation b/w
b/w A
A
and
and B?
B?

>>[height,
>>[height, width]=size(A)
width]=size(A)

>>A=rand(4,4);
>>A=rand(4,4);

>>B=A(1:2:height,
>>B=A(1:2:height, 1:2:width);
1:2:width);

>>B=A([1
>>B=A([1 44 33 2],
2], :);
:);

Matrix Operation (4)


Deleting a row from matrix using []
A(i,:) = [] delete row i from matrix
A(:, j)=[] delete column j from matrix

Concatenating matrices
Matrices can be created using other
matrices
Example
Example 1:
1:

Example
Example 2:
2:

>>A=[1
>>A=[1 22 ;; 33 4];
4];

>>A=ones(3,3);
>>A=ones(3,3); B
B == zeros(3,3);
zeros(3,3);

>>B=[
>>B=[ A
A 2*A;
2*A; A
A 2*A];
2*A];

>>C=[
>>C=[ A
A B;
B; B
B A];
A];

Basic Plot (1)


Create simple plot using command plot
2D plotting: need to specify data points
using x coordinates and y coordinates
How to use plot:
Example
Example 1:
1:

>>
>> xx == [1
[1 22 33 44 55 6];
6];
>>
>> yy == [3
[3 -1
-1 22 44 55 1];
1];
>>
>> plot(x,
plot(x, y,
y, o)
o)
X coordinates

format

y coordinates

4
3
2
1
0
-1

Basic Plot (2)


Format of plotting
The format can be represented by up to 3 chars to
specify color, plot symbol and line type:
Color
Color
yy
m
m
cc
rr
gg
bb
w
w
kk

yellow
yellow
magenta
magenta
cyan
cyan
red
red
green
green
blue
blue
white
white
black
black

Symbol
Symbol

line
line type
type

.. point
-point
oo circle
::
circle
xx x-mark
-.-.
x-mark
++ plus
-plus
-** star
star
ss square
square
dd diamond
diamond
vv triangle
triangle (down)
(down)
^^ triangle
triangle (up)
(up)
<< triangle
triangle (left)
(left)
>> triangle
triangle (right)
(right)
pp pentagram
pentagram
hh hexagram
hexagram

solid
solid
dotted
dotted
dashdot
dashdot
dashed
dashed

Example:to plot red


solid line with circle
to indicate data
point:
>>plot(x,y,ro-);

Basic Plot (3)


Adding titles, axis labels, and annotations
Add title using command title as:
>>title(Title of the figure);

Add axis labels using the following commands


For x-axis: xlabel(Label of x-axis);
For y-axis: ylabel(label of y-axis);
Example:
>>x=0:pi/10:2*pi;
>>plot(x, sin(x));
>> xlabel('x = 0:2\pi')
>> ylabel('Sine of x')
>> title('Plot of the Sine function')

Basic Plot (4)


Multiple data sets in one plot
Method 1:
>>plot(x1,y1,'
--'
,x2,y2,'
-'
,x3,y3,'
:'
)
Data set 1

Data set 2

Data set 3

Method 2: using hold on


Example:
>>plot(x1,y1);
>>hold on
>> plot(x2,y2,ro-);
>> hold on
>> plot(x3,y3,g:);

Add legend for annotation


>>legend(data set 1,data set 2,data set3);

Basic Plot (5)


Example:
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,'
--'
,x,y2,'
-'
,x,y3,'
:'
)
>> xlabel('
0 \leq x \leq 2\pi'
)
>> ylabel('
Cosine functions'
)
>> legend('
2*cos(x)'
,'
cos(x)'
,'
0.5*cos(x)'
)
>> title('
Typical example of multiple plots'
)
>> axis([0 2*pi -3 3])

Basic Plot (5)


Multiple data sets in subplots
Using command subplot to line up multiple plots
in one figure
subplot(m,n,k);
Divide figure
into m*n array

Position of current
subplot

Example:
Example: For
For the
the 33 data
data sets
sets on
on the
the
previous
previous example,Using
example,Using subplot
subplot to
to
plot
plot them
them separately
separately
>>
>> subplot(3,1,1);
subplot(3,1,1);
>>
>> plot(x,y1);
plot(x,y1);
>>
>> subplot(3,1,2);
subplot(3,1,2);
>>plot(x,y2);
>>plot(x,y2);
>>
>> subplot
subplot (3,1,3);
(3,1,3); plot(x,y3);
plot(x,y3);

Potrebbero piacerti anche