Matlab PPT - Session 1 - Week 4
Matlab PPT - Session 1 - Week 4
WEEK 4
MATRICES GENERATION
Results:
Remember when
expression, the
mentioned first,
column number.
This feature also
original matrix.
For example:
>>x(3,3)=x(1,3)
column
no.
row no.
>> x2 = [z,x(:,2:4)]
or
>> x2 = [z,x(:,2:end)]
>> x3 = [x(:,1),z,x(:,3)]
>> x4 = [x(:,1:2),z]
Example 1
Generate the following matrix and
store it as variable example.
[3 5 1; 1 2 5; 6 1 0]
2) Append the following column of data
to the matrix
[1 2 4]
3) Set the values of the first column of
example equal to the values in the
third column
1)
EXAMPLE 1:
Matrix Division
Let
>>x=[1;0;2];
>>y=[2;0;4]
We know y=mx, then what is m?
Then m=y x,
>>m=x\y
Example 2
25
1.5
7.5
30
20
1.5
5.5
35
8.5
Example 2:
Array operations
>>
>>
>>
>>
>>
a = [1 2 3];
b = [4 5 6];
c = a.*b
d = c./b
e = d.^a
Vector generation
>> a = 1:5
Vector with increments
o >> a = 1: 0.5: 5
>> x = [0:pi/8:pi];
>> y = sin (x);
>> z = [x y]
To manipulate matrices;
>>a = z(1:5,2)
Logical/Relational Operators
= equal to
-= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
>> x = [1 0 2 1 3 15 1 0];
>> y = x(x<=3*std(x))
Removes no15 as an outlier; from the
standard deviation for this matrix, std =
4.9982
>> s = x<=3*std(x)
Shows vector matrix in the form of 1 and
0; where 0 indicates the outlier
elements.
>>x(6)=[ ]
removing the outlier; position #6 in the
matrix
>>a(:,[2 4])=[ ]
removing entire row and column 2 and
4
Matrix manipulation
function
diag
fliplr
flipup
reshape
rot90
>>
>>
>>
>>
>>
a = [1 2 3; 4 5 6; 7 8 9]
f = diag(a)
f=fliplr(a)
f=flipud(a)
f=rot90(a)
>>e=ones(10)
Producing all ones in 10 rows and 10
columns
>>e=zeros(5,2)
Producing all zeros in 5 rows and 2
columns
>>e=ones (size(a))
Producing all ones in the same size as
rows and columns of variable a
>>b=[a,ones(size(a));a,a]
Alternatively;
>> s=ones (size(a))
>> s=s*diag(b)
>> c=a-s
Example 3
1)
10
13
11
16
13
19
Example 3
Example 3
(1)
(2)
(3).
(4).