Sunteți pe pagina 1din 20

Praktikum 3

Multiplication With 2D Arrays


If two 2D arrays are multiplied with
the * operator the first array must
have the same number of columns
as the second arrays has rows.
mxp

C = A*B

mxn

nxp

B
2

Multiplication With 2D Arrays


Example:
3 1 0
A

1 2 4

2
B 4
1

3 2 1 4 0 1
C A* B

1 2 2 4 4 1
10

Systems of Linear Equations


Many engineering problems can be
modelled by a system of linear
equations.
Example: Traffic Flow Modelling
500 cars/hour
700 cars/hour

x1 cars/hour
x2 cars/hour
400 cars/hour
600 cars/hour

All streets are one


way. Labels
denote traffic flow
on each segment
of the road.
4

Traffic Flow Modelling


If we want to find x1 and x2 from
the known traffic flow data we can
formulate the problem as a system
of linear equations by balancing the
flow of cars into and out of each
intersection.
x1 x2 500 700
x2 600 400

or

1 1 x1 1200
0 1 x 1000

Traffic Flow Modelling


To solve this problem in Matlab we
can use the left division operator:
>> A = [1, 1; 0, 1;]
>> b = [1200; 1000;]
>> x = A\b

The result is x1=200 and x2=1000


cars/hour.
6

Matrix Inverse and Determinant


You can also solve any Ax=b type
problem by finding A-1 and
determining x=A-1*b. This is
usually inefficient.
If you do want to obtain the inverse
of a inverse in Matlab you could use
the inv command eg C=inv(A)
If you need the determinant of a
matrix use det(A)
7

The identity matrix


The n n identity matrix is a
square matrix with ones on the
diagonal and zeros everywhere
else.
It is called the identity because it
plays the same role that 1 plays in
multiplication, i.e.
AI = A, IA = A, Iv = v
Command:
8

Some other useful commands


C=rand (5,5)
random matrix with uniform
distribution

size (C)
gives the dimensions (m n) of C

max (C)
the maximum of each column

min (C)
the minimum in each column
9

sum (C)
sums each column

mean (C)
the average of each column

diag (c)
just the diagonal elements

C
tranpose the matrix
10

Matrices
A = pascal(3)
B = magic(3)
C = ones(3)
D = chol(A)
See help to find out more

11

Element-wise Multiplication
With 2D Arrays
In some applications we want to
perform an element-wise
multiplication, i.e. multiply each
element in the first array by the
corresponding element in the
second array. (In this case the two
arrays must be the same size.)

12

Contaminant Example
An environmental engineer is assessing
the levels of contaminant in the soil on
a polluted site. The contaminated area
has been divided into a grid and the
level of contaminant (C) has been
measured in each rectangle in the grid.

This data can be stored


as a 2D array and
analysed in Matlab.

grid of sampling locations


contaminated site

13

Contaminant Example
The array of contaminant levels (C)
has 6 rows and 5 columns so it will
be indexed with two indices. The
first index runs from 1 to 6, and
the second index runs from 1 to 5.

14

Contaminant Example
The data is entered with commas
separating columns, semi-colons
separating rows.

>> C = [ 0.002, 0.005, 0.004, 0.007, 0.006;


0.003, 0.001, 0.008, 0.009, 0.010;
0.002, 0.003, 0.006, 0.009, 0.008;
0.001, 0.002, 0.005, 0.008, 0.007;
0.001, 0.002, 0.004, 0.005, 0.003;
0.002, 0.001, 0.004, 0.003, 0.002]
15

Contaminant Example
What is the total amount of
contaminant contained in the
polluted soil?
The concentration values have
units of g/cm3, so to work out the
total mass of contaminant in the
soil we need to first work out the
total volume of soil in each cell.
16

Contaminant Example
At this site we have an array of
data with the soil depth (in cm) in
each grid cell (depth to bedrock).
>> Depth = [ 285, 310, 320, 315, 300;
275, 305, 310, 320, 295;
270, 300, 300, 310, 280;
260, 290, 280 ,270, 255;
255, 285, 270, 265, 250;
250, 280, 265, 260, 240]

17

Element-wise Multiplication
To perform multiplication elementwise use a . before the operator,
e.g.:
3 1 0
4 2 1
A
B

1 2 4
0 1 3
3 4 1 2 0 1
C A. * B

1
4

12 2 0

2
12

18

Contaminant Example Solution


The size of each cell is 2m by 2m.
So the area of each cell is 40,000
cm2.
To calculate the mass of
contaminant present in each cell of
the contaminated zone we could
enter:
Mass = 40000*C.*Depth
19

20

S-ar putea să vă placă și