Download Powerpoint

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Week 3
• Last week
• Vectors
• Array Addressing
• Mathematical Operations
•
•
•
•
Array Multiplication and Division
Identity Matrix
Inverse of a Matrix
Element by Element Calculations
Array Addressing
A=
1
4
7
2
5
8
3
6
9
>> B = A([2 3],[2 3])
B=
5
8
6
9
>> B = A(2:3,2:3)
B=
5
8
6
9
Vectors
>> q = 0:0.1:2*pi; % semicolon suppresses output
>> y=sin(q);
% Comment after % is ignored
>> plot(q,y)
Transpose
Interchange rows and columns
>> X = [1 2 3 4 5
6 7 8 9 10]
X=
1 2 3 4
6 7 8 9
>> X'
ans =
1 6
2 7
3 8
4 9
5 10
5
10
Vector Functions
A = [ 1 2 3 4 5 6]
sum(A) = 21
max(A) = 6
min(A) = 1
Random Arrays
>> A = rand(4,3)
A=
0.9501
0.2311
0.6068
0.4860
0.8913
0.7621
0.4565
0.0185
0.8214
0.4447
0.6154
0.7919
Sound Files
sound_in.m creates an array called data.
data is an array of one column with 10,000 rows
Sounds can be saved under different names
Yes = data;
and = data;
No = data;
Arrays can be concatenated to form longer sounds
yesandNo = [Yes; and; No]
Array Multiplication
• Y = A*B
• The number of columns in A must equal the number
of rows in B.
Multiplication Example
•
•
•
•
•
•
•
•
C(1,1) = 1*1 + 2*2 + 3*3 = 14
C(1,2) = 1*2 + 2*1 + 3*1 = 7
C(2,1) = 3*1 + 2*2 + 1*3 = 10
C(2,2) = 3*2 + 2*1 + 1*1 = 9
C(3,1) =
C(3,2) =
C(4,1) =
C(4,2) =
Errors
A= 1
4
7
2
5
8
3
6
9
>> C= ones(3,2)
C= 1 1
1 1
1 1
>> A*C
ans =
6 6
15 15
24 24
>> C*A
??? Error using ==> *
Inner matrix dimensions must agree.
Example
>> BV = [3; 1; 4]
BV =
3
1
4
>> AV = [2 5 1]
AV =
2 5 1
>> AV*BV
ans =
15
>> BV*AV
ans =
6 15 3
2 5 1
8 20 4
% AV*BV is the dot product
% The number of columns in
% AV equals the number of
% rows in BV
% AV*BV is a scalar
% Rows in BV = 3
% Columns in AV = 3
% BV&AV is a 3x3 array
Identity Matrix
>> I = eye(3)
I=
1 0 0
0 1 0
0 0 1
Identity Matrix
• Multiplying a
matrix by the
identity matrix is
equivalent to
multiplication by
one
>> I = eye(3)
I=
1 0 0
0 1 0
0 0 1
If A is square
AI = IA = A
Inverse of a Matrix
• The matrix B is the
inverse of the matrix
A if
• BA = AB = I
• where I is the
identity matrix
•
•
•
•
•
•
•
•
•
•
•
•
>> A =[ 1 2; 2 3]
A=
1 2
2 3
>> B = A^-1
B=
-3 2
2 -1
>> A*B
ans =
1 0
0 1
Array Division
Solve for X where
A*X = B
A and B are known arrays.
A^-1 *A*X = A^-1 *B
Since A^-1* A = I
X = A^-1* B
Left Division
X = A\B
Division Example
•
• 2x - 3y + 4z = 10
• x + 6y - 3z = 4
• -5x + y + 2z = 3
• A*X = B
• where
•
•
•
•
•
•
•
•
•
•
•
2 -3 4
1
6 -3
-5 1 2
B = 10
4
3
>> X = A\B
X=
1.2609
2.2261
3.5391
>> 2*X(1) -3*X(2) + 4*X(3)
ans = 10
A=
Element by Element Operations
• If the usual symbols (* / ^) are used operations follow
the rules of linear algebra. Sometimes we don’t want
this.
• Given x = [1 2 3]
• And y = [2 4 6]
• Find z = [x(1)*y(1), x(2)*y(2), x(3)*y(3)]
• Use the dot product operator .*
• z = x .*y = [2 8 18]
Element by Element Operators
•
•
•
•
Multiplication
Division
Exponentiation
Left Division
•
•
•
•
x = A .*B
x = A ./ B
x = A .^ y
x = A .\B
.*
./
.^
.\
x(n) = A(n)*B(n)
x(n) = A(n)/B(n)
x(n) = A(n)^y(n)
x(n) = B(n)/A(n)
Element by Element Example
A=[0
1
2
3
4]
B=[5
4
3
2
1]
>> A .*B
ans =
0 4
6
6
>> A ./B
ans =
0 0.2500
>> A .^2
ans =
0 1
4
9
4
0.6667
16
1.5000
4.0000
Element by Element Calculation
• >> x= 0:0.1:10;
• >> y = x .^2;
• >> plot(x,y)
Related documents