MATLAB– MATRIX 專專 ME2002-2010Fall (week06- Oct27) 專專專專 : NTU- 初初 MATLAB2(A++)-for-Week0506.ppt
MATLAB– MATRIX 專論
ME2002-2010Fall (week06- Oct27)原始檔案 : NTU- 初探 MATLAB2(A++)-for-Week0506.ppt
1. array multiplication (element-by-element multiplication),
2. matrix multiplication.
兩種乘法的定義
[1 2][3
4]
兩純量之間的運算
兩陣列與矩陣之間的運算
Symbol
+
-
+
-
.*
./
.\
.^
Examples
[6,3]+2=[8,5]
[8,3]-5=[3,-2]
[6,5]+[4,8]=[10,13]
[6,5]-[4,8]=[2,-3]
[3,5].*[4,8]=[12,40]
[2,5]./[4,8]=[2/4,5/8]
[2,5].\[4,8]=[2\4,5\8]
[3,5].^2=[3^2,5^2]
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Operation
Scalar-array addition
Scalar-array subtraction
Array addition
Array subtraction
Array multiplication
Array right division
Array left division
Array exponentiation
Form
A + b
A – b
A + B
A – B
A.*B
A./B
A.\B
A.^B
Element-by-element operations
Array or Element-by-element multiplication is defined only for arrays having the same size. The definition of the product x.*y, where x and y each have n elements, is
x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)]
if x and y are row vectors. For example, if
x = [2, 4, – 5], y = [– 7, 3, – 8]
then z = x.*y gives z = [2(– 7), 4 (3), –5(–8)] = [–14, 12, 40]
Element-by-element multiplication
If x and y are column vectors, the result of x.*y is a column vector. For example z = (x’).*(y’) gives
Note that x’ is a column vector with size 3 × 1 and thus does not have the same size as y, whose size is 1 × 3.
Thus for the vectors x and y the operations x’.*y and y.*x’ are not defined in MATLAB and will generate an error message.
2(–7)4(3)
–5(–8)
–141240
=z =
Element-by-element multiplication
The array multiplication operation A.*B results in a matrix C that has the same size as A and B and has the elements ci j = ai j bi j . For example, if
then C = A.*B gives this result:
A = 11 5 –9 4
B = –7 8 6 2
C = 11(–7) 5(8) –9(6) 4(2)
= –77 40–54 8
Element-by-element multiplication
The symbol for array right division is ./. For example, if
x = [8, 12, 15] y = [–2, 6, 5]
then z = x./y gives
z = [8/(–2), 12/6, 15/5] = [–4, 2, 3]
Array Division
A = 24 20– 9 4
B = –4 5 3 2
Also, if
then C = A./B gives
C = 24/(–4) 20/5 –9/3 4/2
= –6 4–3 2
練習A=[1 0 ; 2 1] B=[-1 2; 0 1]C=[3; 2]D=5
(a) A+B=?(b) A.* B=?(c) A*B=?(d) A*C=?(e) A+C=?(f) A+D=?(g) A.*D=?(h) A*D=?
(a) [0 2;2 2](b) [-1 0;0 1](c) [-1 2;-2 5](d) [3;8](e) NA(f) [6 5;7 6](g) [5 0;10 5](h) [5 0;10 5]
6x + 12y + 4z = 707x – 2y + 3z = 52x + 8y – 9z = 64 >>A = [6,12,4;7,-2,3;2,8,-9];>>B = [70;5;64];>>Solution = A\BSolution = 3 5 -2
The solution is x = 3, y = 5, and z = –2.
Solution of Linear Algebraic Equations
A X = B
X = A-1 B
範例• 五條卡車貨運路線的行駛距離與行駛時間如下表:請
問有最高行車速率的路線是哪一條?速率為多少?
1 2 3 4 5
距離 miles 560 440 490 530 370
時間 hrs 10.3 8.2 9.1 10.1 7.5
>>d= [560, 440, 490, 530, 370]; >>t=[10.3,8.2,9.1, 10.1, 7.5]; >>spd = d ./ t
範例• 電阻 (R) 與電壓 (V) 數據如下表,請其出各自
的電流 (I=V/R) 與消耗的功率 (P=V2/R).
1 2 3 4 5R (Ohms) 104 2x104 3.5x10
4
105 2x105
V (volts) 120 80 110 200 350
R=[…]; V=[..]; // fill in the data , not BLNAK. Current=V . / R; Power=V.^2 ./ R;
To perform exponentiation on an element-by-element basis, we must use the .^ symbol. For example,
if x = [3, 5, 8], then typing x.^3 produces thearray [33, 53, 83] = [27, 125, 512].
if p = [2, 4, 5], then typing 3.^p produces the array [32, 34, 35] = [9, 81, 243].
Array Exponentiation
3.^p3.0.^p3..^p(3).^p3.^[2,4,5]
結果都一樣你看得出來嗎?
範例• 依據牛頓運動定律,初速 v ,仰角 θ ,物
體的最高高度為 h = v2*sin2(θ)/(2*g)• 請建立一個表格,說明不同 v 與 θ 之下的
h 。• 其中 v=10,12,14,16,18,20 m/s• θ=50, 60, 70, 80 degree
範例v= [10:2:20]; th=[50:10:80];thr=th*(pi/180);g=9.8;vel=[];%create the 6x4 array of speedsfor k=1:length(thr)
vel= [vel,v’];endtheta=[];%create the 6x4 array of anglesfor k=1:length(v)
Theta=[theta;thr];endh=(vel.^2.*(sin(theta)) .^ 2 )/ (2*g);h= [v’,h];table=[0,th;H)
In the product of two matrices AB, the number of columns in A must equal the number of rows in B.
The row-column multiplications form column vectors, and these column vectors form the matrix result. The product AB has the same number of rows as A and the same number of columns as B.
For example,
6 –2 10 3 4 7
9 8–5 12
= (6)(9) + (– 2)(– 5) (6)(8) + (– 2)(12) (10)(9) + (3)(– 5) (10)(8) + (3)(12) (4)(9) + (7)(– 5) (4)(8) + (7)(12)
64 24 75 116 1 116
=
Matrix Multiplication
Use the operator * to perform matrix multiplication in MATLAB.
>>A = [6,-2;10,3;4,7];>>B = [9,8;-5,12];>> A*Bans = 64 24 75 116 1 116
Matrix Multiplication
範例• 有三個產品均需要經過四道工序,所需時間與各工
序的單位時間成本如下表,請問每一產品的生產成本各是多少?生產 10/5/7 個產品 1/2/3 故需多少成本?
產出一單位產品需要的時間, hrs
工序 成本 $ /hr 產品 1 產品 2 產品 3
車削 10 6 5 4
研磨 12 2 3 1
銑 14 3 2 5
焊接 9 4 0 3
範例• A=[10,12,14,9];% hourly cost• B=[6 2 3 4; 5 3 2 0; 4 1 5 3]; %time required• unitCst=A*B’ %answer is [162 114 149]• C=[10 5 7];%No. of items• totalCst=C*unitCst’%answer is 3233
範例
U=[6,2,1;2,5,4;4,3,2;9,7,3];% 4 x 3 matrixP=[10,12,13,15;8,7,6,4;12,10,13,9;6,4,11,5];% 4 x 4 matrixC=U’*PQuarterly_cst=sum(C)%answer=[400 351 509 355]Category_cst=sum(C’)%answer=[760 539 316]
單位成本 $x103
產品 材料 人工 運輸1 6 2 1
2 2 5 4
3 4 3 2
4 9 7 3
各季產量
產品 第 1季
第 2 季 第 3 季 第 4季
1 10 12 13 15
2 8 7 6 4
3 12 10 13 9
4 6 4 11 5
that is, in general, AB ¹ BA. A simple example will demonstrate this fact:
AB = 6 –210 3
9 8–12 14
= 78 2054 122
BA = 9 8–12 14
6 –210 3
= 134 6 68 65
whereas
Reversing the order of matrix multiplication is a common and easily made mistake.
Matrix multiplication does not have the commutative property
•the null or zero matrix, denoted by 0 •the identity, or unity, matrix, denoted by I.
The null matrix contains all zeros and is not the same as the empty matrix [ ], which has no elements.
These matrices have the following properties:
0A = A0 = 0
IA = AI = A
Two exceptions to the noncommutative property
is a square matrix whose diagonal elements are all equal to one, with the remaining elements equal to zero.
For example, the 2 × 2 identity matrix is
I = 1 00 1
The functions eye(n) and eye(size(A)) create an n × n identity matrix and an identity matrix the same size as the matrix A.
The identity matrix
建立大小為 m × n 的矩陣>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]; % 建立 3×4 的矩陣 A>> A % 顯示矩陣 A 的內容A = 1 2 3 4 5 6 7 8 9 10 11 12
m x n 矩陣的各種處理• % 將矩陣 A 第二列、第三行的元素值,改變為 5
• >> A(2,3) = 5 • A = 1 2 3 4 5 6 5 8 9 10 11 12
• % 取出矩陣 A 的第二橫列、第一至第三直行,並儲存成矩陣 B
• >> B = A(2,1:3) B = 5 6 5
m x n 矩陣的各種處理 ( 續 1)• % 將矩陣 B 轉置後、再以行向量併入矩陣 A• >> A = [A B'] A = 1 2 3 4 5 5 6 5 8 6 9 10 11 12 5
• % 刪除矩陣 A 第二行(:代表所有橫列, [] 代表空矩陣)• >> A(:, 2) = [] A = 1 3 4 5 5 5 8 6 9 11 12 5
m x n 矩陣的各種處理 ( 續 2)• >> A = [A; 4 3 2 1] % 在原矩陣 A 中,加入第四列 A = 1 3 4 5 5 5 8 6 9 11 12 5 4 3 2 1
• >> A([1 4], :) = [] % 刪除第 1, 4 列(:代表所有直行, [] 是空矩陣) A = 5 5 8 6 9 11 12 5
Colon (:) Command
• >>clear; clc;• >>x=0:1:10;• >>A=magic(x(2));• >>B=A(:,2);• >>C=A(2,:);• >>D=A(2,1:2);• >>E=[A B];• >>F=[A;C];
請寫下由 A 到 F 的矩陣的內容
Array Addressing
The colon operator selects individual elements, rows, columns, or ''subarrays'' of arrays. Here are some examples:
• v(:) represents all the row or column elements of the vector v.
• v(2:5) represents the second through fifth elements; that is v(2), v(3), v(4), v(5).
• A(:,3) denotes all the elements in the third column of the matrix A.
• A(:,2:5) denotes all the elements in the second through fifth columns of A.
• A(2:3,1:3) denotes all the elements in the second and third rows that are also in the first through third columns.
You can use array indices to extract a smaller array from another array. For example, if you first create the array B
B =
C =16 3 7 8 4 9
2 4 10 1316 3 7 18 8 4 9 25 3 12 15 17
then type C = B(2:3,1:3), you can produce the following array:
m x n 矩陣的各種處理 ( 續 3)
• array1=[1.1 -2.2 3.3 -4.4 5.5];• array1(3)=? [3.3]• array1([1 4])=? [1.1 -4.4]• array1(1:2:5)=? [1.1 3.3 5.5]
• arr2=[1 2 3; -2 -3 -4; 3 4 5];• arr2(1,:)=? [1 2 3]• arr2(:,1:2:3)=? [1 3; -2 -4; 3 5]
m x n 矩陣的各種處理 5• >>arr3=[1 2 3 4 5 6 7 8];• arr3(5:end)=[5 6 7 8]• arr3(end)=[8]
• >>arr4=[1 2 3 4; 5 6 7 8; 9 10 11 12];• arr4(2:end;2:end)=? [6 7 8; 10 11 12]• >>arr4(1:2, [1 4])=[20 21;22 23];• arr4 =? [20 2 3 21; 22 6 7 23; 9 10 11 12]• >>arr4=[20 21; 22 23];• arr4=?
m x n 矩陣的各種處理 6
• >>arr4=[1 2 3 4; 5 6 7 8; 9 10 11 12];• >>arr4(1:2,1:2)=1• arr4= 1 1 3 4 1 1 7 8 9 10 11 12
m x n 矩陣的各種處理 7
• >>arr5=zeros(4)• >>arr6=ones(4)• >>arr7=eye(4) % 單元矩陣• 比較 length(arr?) 與 size(arr?) 的差別• >>arr5=zeros(3,4)• >>arr6=ones(3,4)• >>arr7=eye(3,4)• 比較 length(arr?) 與 size(arr?) 的差別
m x n 矩陣的初始格式化
Array Functionssize(A) Returns a row vector [m n] containing the sizes of the m x n array
A.
sort(A) Sorts each column of the array A in ascending order and returns an array the same size as A.
sum(A) Sums the elements in each column of the array A and returns a row vector containing the sums.
max(A) Returns the algebraically largest element in A if A is a vector. Returns a row vector containing the largest elements in each column if A is a matrix. If any of the elements are complex, max(A) returns the elements that have the largest magnitudes.
min(A) Like max but returns minimum values.
Length(A) Computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m × n matrix.
Advanced Array Functions
[b, k] = sort(A) Sorts each column of the array A in ascending order and returns a row vector b and their indices in the row vector k.
[x, k] = max(A) Similar to max(A) but stores the maximum values in the row vector x and their indices in the row vector k.
[x, k] = min(A) Like max but returns minimum values.
[u,v,w] = find(A) Computes the arrays u and v, containing the row and column indices of the nonzero elements of the matrix A, and the array w, containing the values of the nonzero elements. The array w may be omitted.
max(A) returns the vector [6,2]; min(A) returns the vector [-10, -5]; size(A) returns [3, 2]; length(A) returns 3.sum(A) returns [-1 -3] 得到”行加總”請問如何求列加總 ? sum(A’)’
A =
6 2 –10 0 3 –5
練習
範例• 五條卡車貨運路線的行駛距離與行駛時間如下表:請
問有最高行車速率的路線是哪一條?速率為多少?
1 2 3 4 5
距離 miles 560 440 490 530 370
時間 hrs 10.3 8.2 9.1 10.1 7.5
>>d=[560,440,490,530,370]; >>t=[10.3,8.2,9.1,10.1,7.5]; >>[h_spd, route]=max(d./t)
練習
• sort 指令• a = [92, 95, 58, 75, 69, 82] ,執行 sort 指令: • [b, index] = sort(a) • 會得到
– b = [58, 69, 75, 82, 92, 95] – 及 index = [3, 5, 4, 6, 1, 2]– 其中 index 的每個元素代表 b 的每個元素在 a 的位
置,– 滿足 b 等於 a(index)