Vous êtes sur la page 1sur 67

CHE692 PROCESS

MODELLING AND SIMULATION

WEEK 4

MATRICES GENERATION

Most of the operations performed in


MATLAB require data presented in matrix
form.
For small matrices, it is easy to enter the
matrix explicitly.
In this case, elements are separated by
blanks or commas, individual rows are
separated by semicolon (ie: ;) and all
elements are surrounded by square
parentheses (ie: [ ])

Results:

This matrix is now stored as the


variable x and can be used at a later
date.
MATLAB is case sensitive. So, x is
NOT the same as X.
Large matrices can be entered over a
number of lines.
In this case, the button return is
used to replace the semicolon.

Individual matrix elements can be referred


with indices inside parentheses ( ).
For example:
>>a=x(1,3)

ie: first row, third column

Remember when
expression, the
mentioned first,
column number.
This feature also
original matrix.
For example:
>>x(3,3)=x(1,3)

using this form of


row number is
followed by the
allow to alter the

Further columns and/or rows can be


added to a matrix.
For example, to append a row to a
matrix:
>>y=[10 11 12]
>>x=[x;y]

And to append a column to a matrix;


>> z = [13;14;15 ;16]
>> x1 = [x,z]

A colon : is used to extract small


matrices from large matrices.
For example:
>>x=x(1:3,:) means take the row 1 to
3, and all the elements in every
column. The resulting output will be
the original matrices.

column
no.
row no.

By: Pn. Hazlina Husin

A semicolon ; can be placed at the


end of an expression if you do not
wish the output to be displayed on
the screen.
However, the values would have
been stored and can be displayed
when
you
type
the
variable
representing the values.

Substituting one column in


matrix x

>> x2 = [z,x(:,2:4)]
or
>> x2 = [z,x(:,2:end)]
>> x3 = [x(:,1),z,x(:,3)]
>> x4 = [x(:,1:2),z]

Commands for managing the work session:

clc = clears the command window,


but the
variables remain in the
memory.
clear = removes all variables from
memory.
clear var1 var2 = removes the
variables
var1 and var2 from
memory.
exist(name) = determine if a file or
variable exists having the
name name.

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

Two types of divisons:


Right division: /
Left division: \

X=A/B is the same as X*B=A


o X=A*inverse (B)

X=A\B is the same as A*X=B


o X=inverse (A) * B

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

Consider a very simple process


where 3 input variables; i.e.
temperature (T), pressure (P),
flowrate (F) and a single output
variable, composition (y).
Time, t

25

1.5

7.5

30

20

1.5

5.5

35

8.5

The relationship between these


variables is given by the following;
y = a1*T + a2*P + a3*F
By constructing a matrix containing
the measurements above and using
the \operator, calculate the values of
a1,a2 and a3.

Example 2:

Array operations

Operation for element by element


basis
The dot (.) must be included, i.e. if
the matrix contains only 1 row.
Only applied to multiplication,
division and power operations only.
Subtraction and addition operation:
need not to consider this

>>
>>
>>
>>
>>

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

Vector with negative increments, and


transpose
o >> a = [3:-0.5:1]

Sine function using vector

>> x = [0:pi/8:pi];
>> y = sin (x);
>> z = [x y]
To manipulate matrices;
>>a = z(1:5,2)

Try the below operators;


>> a = z(1:5,:)
>> a = z(:,2)

Consider the following matrix;


>> a = [1 2 3 4;5 6 7 8; 9 10 11 12;13 14
15 16]
>> a(1:3,[2 4])= z(3:5,:)
>> z
What has changed when you assign this
command?

Logical/Relational Operators

= equal to
-= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to

These operators compare matrices


on the either side of the operator.
True = 1
False = 0
Sample of application: removal of
data outliers from data set

>> 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)

Generating Large Matrices

>>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]

>> r = rand (3,3)


Produces 3 rows and 3 columns
containing random numbers between 0
and 1

Subtracting row vector from


matrix

>> a=[1 2 3 4;5 6 7 8;9 10 11 12; 13


14 15 16; 17 18 19 20];
>> b=[1 2 3 4];
>>[row,col]=size (a)
>> e=ones(row,1)
>> d=e*b
>> c=a-d

Alternatively;
>> s=ones (size(a))
>> s=s*diag(b)
>> c=a-s

Example 3
1)

Generate the following matrices by


using the : command to generate
three vectors and then combining
these vectors into one matrix.
1

10

13

11

16

13

19

2) Generate a second matrix of the same


size which contains only the values 1 and
0. A 1 referring to any element in the
above matrix which has a value greater
than 9, and a 0 referring to any element
in the above matrix which has a value
less than or equal to 9.
3) Remove the second column of this new
matrix.
4) Finally generate the transpose of this
original matrix and multiply it by the
original, untranspose matrix.

Example 3

Example 3
(1)

(2)

(3).

(4).

Vous aimerez peut-être aussi