learner’s guide to matlab® chapter 2 : working with arrays
TRANSCRIPT
Learner’s Guide to MATLAB®
Chapter 2 : Working with Arrays
Working with Arrays
Chapter Outline
• Creating Vector and Matrix
• Accessing and Addressing Matrix
• Mathematical Operations with Matrix
• Functions for Analyzing Matrix
• Chapter Case Study
Working with Arrays
Creating Vectors
a
n subdivision points
bdx
>> x = a:dx:b;
>> x = linspace(a,b,n);
>> x = x';
x
row vectors
convert to column vector
Working with Arrays
Creating Vectors - Examples
1) Create a COLUMN VECTOR with the elements :55, 14, log(51), 0, sin(pi/3)
2) Create a ROW VECTOR which the first element is 1, the last element is 33, with a increment of 2 between elements
Working with Arrays
Creating Matrices
>> A = [1,2,3; 4,5,6; 7,8,9];
>> A = [1 2 3; 4 5 6; 7 8 9];
>> A = [1 2 34 5 67 8 9]
>> A = A'
or
or
data entry mode
transpose
Working with Arrays
Array Operations
A = 1 2 3 4 5 6 7 8 9
B = 1 2 4 3 4 5 6 5 4
C = A + B
C =
2 4 7 7 9 11 13 13 13
A = 1 2 3 4 5 6 7 8 9
B = 1 2 4 3 4 5 6 5 4
C = A.*B
C =
1 4 12 12 20 30 42 40 36
Array addition Array multiplication
A = 1 2 3 4 5 6 7 8 9
C = A.^2
C =
1 4 9 16 25 36 49 64 81
Array power
Working with Arrays
Array Multiplication
• Matrices must have the same dimensions
• Dimensions of resulting matrix = dimensions of multiplied matrices
• Resulting elements = product of corresponding elements from the original matrices
>> a = [1 2 3 4; 5 6 7 8];
>> b = [1:4; 1:4];
>> c = a.*b
c =
1 4 9 16 5 12 21 32
>> a = [1 2 3 4; 5 6 7 8];
>> b = [1:4; 1:4];
>> c = a.*b
c =
1 4 9 16 5 12 21 32 c(2,4) = a(2,4)*b(2,4)
Working with Arrays
Matrix Operations
A = 1 2 3 4 5 6 7 8 9
B = 2*A
B =
2 4 6 8 10 12 14 16 18
A = 1 2 3 4 5 6 7 8 9
B = 2 + A
B =
3 4 5 6 7 8 9 10 11
Scalar Multiplication Scalar Expansion
A = 1 2 3 4 5 6 7 8 9
B = 1 2 4 3 4 5 6 5 4
C = A * B
C =
25 25 26 55 58 65 85 91 104
Matrix Multiplication
Working with Arrays
>> e=[1 2;3 4] + 5
1 2 = + 5 3 4
1 2 5 5 = + 3 4 5 5
6 7= 8 9
>> e=[1 2;3 4] + 5
1 2 = + 5 3 4
1 2 5 5 = + 3 4 5 5
6 7= 8 9
Matrix Calculation-Scalar Expansion
>> e=[1 2;3 4] + 5e = 6 7 8 9
>> e=[1 2;3 4] + 5e = 6 7 8 9
Scalar expansion
Working with Arrays
Matrix Multiplication
• Inner dimensions must be equal.
• Dimension of resulting matrix = outermost dimensions of multiplied matrices.
• Resulting elements = dot product of the rows of the 1st matrix with the columns of the 2nd matrix.
>> a = [1 2 3;4 5 6];
>> b = [3,1;2,4;-1,2];
>> c = a*b
c =
4 15 16 36
>> a = [1 2 3;4 5 6];
>> b = [3,1;2,4;-1,2];
>> c = a*b
c =
4 15 16 36
[2x3]
[3x2]
[2x3]*[3x2] [2x2]
a(2nd row).b(2nd column)
Working with Arrays
Array Addressing
m(2:4,3)
m(3,1)
Working with Arrays
More Example on indexing
>> a=[3 11 6; 4 7 10; 13 9 0]
a =
3 11 6
4 7 10
13 9 0>> a(3,1)= 20a = 3 11 6 4 7 10 20 9 0>> a(2,3)-a(1,2)ans = -1
>> a=[3 11 6; 4 7 10; 13 9 0]
a =
3 11 6
4 7 10
13 9 0>> a(3,1)= 20a = 3 11 6 4 7 10 20 9 0>> a(2,3)-a(1,2)ans = -1
Create a 3 x 3 matrixUse square brackets [ ]
Matrices must be rectangular. (Undefined elements set to zero)
Assign a new value to the (3,1) element
Use elements in a mathematical expression
Working with Arrays
More Example on Colon indexing
>> a=[3 11 6; 4 7 10; 13 9 0]
a =
3 11 6
4 7 10
13 9 0>> b = a(:,3)b = 6 10 0 >> c = a(2,:)c =
4 7 10>> d = a(2:3,1:2)d = [4 7 ] [13 9 ]
>> a=[3 11 6; 4 7 10; 13 9 0]
a =
3 11 6
4 7 10
13 9 0>> b = a(:,3)b = 6 10 0 >> c = a(2,:)c =
4 7 10>> d = a(2:3,1:2)d = [4 7 ] [13 9 ]
Create a 3 x 3 matrix
Define a column vector b from elements in all rows of column 3 in matrix a
Define a row vector c from elements in all columns of row 2 in matrix a
Create a matrix d from elements in rows 2&3 and columns 1&2 in matrix a
Working with Arrays
• Solve this set of simultaneous equations
Array Division using “Left Division”
>> A = [-1 1 2; 3 -1 1;-1 3 4];
>> b = [2;6;4];
>> x = inv(A)*b
x =
1.0000
-1.0000
2.0000
>> x = A\b
x =
1.0000
-1.0000
2.0000
>> A = [-1 1 2; 3 -1 1;-1 3 4];
>> b = [2;6;4];
>> x = inv(A)*b
x =
1.0000
-1.0000
2.0000
>> x = A\b
x =
1.0000
-1.0000
2.0000
-x1 + x2 + 2x3 = 2
3x1 - x2 + x3 = 6
-x1 + 3x2 + 4x3 = 4
Working with Arrays
Function Description Example
C=max(A) If A is vector, C is the largest element in A
A = [5 9 2]
C = max(A)
sum(A) If A is vector, returns the sum of elements of A
A = [5 9 2]
sum(A)
sort(A) If A is vector, arranges elements of vector in ascending order
A = [5 9 2]
sort(A)
det(A) Returns the determinant of a square matrix A
A = [2 4; 3 5];
det (A)
inv(A) Returns the inverse of a square matrix A
A = [2 4; 3 5];
inv(A)
Functions for Analyzing Matrix
Working with Arrays
Sample Problem 2 : Friction Experiment
The coefficient of friction, μ, can be determined in an experiment by measuring the force F required to move a mass m. When F is measured and m is known, the coefficient of friction can be calculated by:
µ = F / (mg) where g = 9.81 m/s2
Results from measuring F in six tests are given in the table below. Determine the coefficient of friction in each test, and the average from all tests.
Test # 1 2 3 4 5 6
Mass m (kg) 2 4 5 10 20 50
Force F (N) 12.5 23.5 30 61 117 294
Working with Arrays
Summary
• Creating Vector and Matrix
• Accessing and Addressing Matrix
• Mathematical Operations with Matrix
• Functions for Analyzing Matrix