Lecture 2 Matrix manipulations Submatrices Reproduction Reshape Determinant
Lecture 2 Matrix manipulations ØSub-matrices ØReproduction ØReshape ØDeterminant, inversion Flow control Øif, if else, find 1 軟體實作與計算實驗
Matrices ß ß A= [1 2 3 4; 5 6 7 8] reshape(1: 8, 4, 2)' 2 軟體實作與計算實驗
Sub-matrix ß ß a=1: 1: 36 A=reshape(a, 4, 9) % Extract rows 2: 4 and columns 5: 8 of A A(2: 4, 5: 8) 5 軟體實作與計算實驗
Reshape v=1: 12; reshape(v, 3, 4) ans = 1 2 3 4 5 6 7 8 9 10 11 12 6 軟體實作與計算實驗
Reshape ß ß ß a=1: 1: 36 m=4; n=9 A=reshape(a, 4, 9) % reshape vector a to matrix A % A is composed of m rows and n columns 7 軟體實作與計算實驗
Gray image cmw 2. bmp I=imread('cmw 2. bmp'); imshow(I) 8 軟體實作與計算實驗
Sub-matrices I=imread('cmw 2. bmp'); imshow(I); [m, n]=size(I); m 2=ceil(m/2); n 2=ceil(n/3*2); Is=I(1: m 2, 1: n 2); imshow(Is) 9 軟體實作與計算實驗
Repeat I 2=repmat(Is, 2, 3); imshow(I 2); 10 軟體實作與計算實驗
Repmat ß ß ß A=[1 2; 3 4] m=2; n=3; repmat(A, m, n) % repeat A vertically m times % repeat the result horizontally n times 11 軟體實作與計算實驗
Get Rows ß ß A=reshape(1: 16, 4, 4) a=3; b=1; c=2; A([a b c], : ) % get rows specified by [a b c] 12 軟體實作與計算實驗
Get Columns ß ß A=reshape(1: 16, 4, 4) a=3; b=1; c=2; A(: , [a b c]) % get columns specified by [a b c] 13 軟體實作與計算實驗
Row exchange V=reshape(1: 12, 3, 4) V([3 2], : )=V([2 3], : ) V= 1 3 2 4 6 5 7 9 8 10 12 11 14 軟體實作與計算實驗
Column exchange V=reshape(1: 12, 3, 4) V(: , [2 3])=V(: , [3 2]) V= 1 2 3 7 8 9 4 5 6 10 11 12 15 軟體實作與計算實驗
Column sum V=reshape(1: 12, 3, 4) V= 1 2 3 4 5 6 7 8 9 10 11 12 >> sum(V) ans = 6 15 24 33 16 軟體實作與計算實驗
Column sum V=reshape(1: 12, 3, 4) V= 1 2 3 4 5 6 7 8 9 10 11 12 >> sum(V, 1) ans = 6 15 24 33 17 軟體實作與計算實驗
Row sum V=reshape(1: 12, 3, 4) V= 1 2 3 4 5 6 7 8 9 10 11 12 >> sum(V, 2) ans = 22 26 30 18 軟體實作與計算實驗
Mean of columns V=reshape(1: 12, 3, 4) V= 1 2 3 4 5 6 7 8 9 10 11 12 8 11 >> mean(V) ans = 2 5 19 軟體實作與計算實驗
Mean of rows V=reshape(1: 12, 3, 4) V= 1 2 3 4 5 6 7 8 9 10 11 12 >> mean(V, 2) 20 軟體實作與計算實驗
Inversion A=reshape(1: 4, 2, 2); B=inv(A) B= -2. 0000 1. 5000 1. 0000 -0. 5000 21 軟體實作與計算實驗
inv A=[2 1 -1; -3 -2 5; 1 1 1]; inv(A)*b b=[1 0 5]' ans = -1. 6000 5. 4000 1. 2000 23 軟體實作與計算實驗
Left devision A=[2 1 -1; -3 -2 5; 1 1 1]; b=[1 0 5]' ß ß ß Ab % Left devision % Ax=b could be solved by left devision 24 軟體實作與計算實驗
Determinant A=reshape(1: 4, 2, 2); >> det(A) ans = -2 25 軟體實作與計算實驗
Area of a parallelogram • v 1, v 2 denotes two vectors in a plane • Calculate the area of the parallelogram determined by v 1 and v 2 26 軟體實作與計算實驗
Volume of a parallelepiped A=[v 1; v 2; v 3] det(A) 27 軟體實作與計算實驗
reshape(randperm(9), 3, 3) ans = 5 2 7 9 1 3 6 8 4 28 軟體實作與計算實驗
Append Horizontally >> A=reshape(randperm(9), 3, 3); >> B=reshape(randperm(9), 3, 3); >> C=[A B] C= 6 3 8 2 4 7 9 5 1 5 6 8 4 3 1 29 9 7 2 軟體實作與計算實驗
Append Vertically >> C=[A; B] A= 6 3 8 5 6 8 2 4 7 4 3 1 9 5 1 9 7 2 30 軟體實作與計算實驗
if false condition true statements 31 軟體實作與計算實驗
if else false condition true statements 32 statements 軟體實作與計算實驗
- Slides: 32