Lecture 2 Matrix representation Vectors 2 D matrices

  • Slides: 42
Download presentation
Lecture 2 Matrix representation ØVectors Ø 2 D matrices Ø 3 D matrices Matrix

Lecture 2 Matrix representation ØVectors Ø 2 D matrices Ø 3 D matrices Matrix manipulation 軟體實作與計算實驗 1

Algebraic vectors Creation n n linspace rand one zeros 軟體實作與計算實驗 2

Algebraic vectors Creation n n linspace rand one zeros 軟體實作與計算實驗 2

linspace a=linspace(0, 1, 5) Create 5 knots that equally partition interval [0 1] 軟體實作與計算實驗

linspace a=linspace(0, 1, 5) Create 5 knots that equally partition interval [0 1] 軟體實作與計算實驗 3

reverse a=1: 9; n=length(a); b=a(n: -1: 1); 軟體實作與計算實驗 4

reverse a=1: 9; n=length(a); b=a(n: -1: 1); 軟體實作與計算實驗 4

Transpose A= A=[1 2 3; 4 5 6; 7 8 9]; A' ans =

Transpose A= A=[1 2 3; 4 5 6; 7 8 9]; A' ans = 1 2 3 4 5 6 1 4 7 2 5 8 3 6 9 7 8 9 軟體實作與計算實驗 5

Example A=[1 2 3; 4 5 6; 7 8 9]; A(: , 3: -1:

Example A=[1 2 3; 4 5 6; 7 8 9]; A(: , 3: -1: 1) ans = 3 6 9 2 5 8 A= 1 4 7 2 5 8 3 6 9 1 4 7 軟體實作與計算實驗 6

Example A=[1 2 3; 4 5 6; 7 8 9]; B=A'; B(: , 3:

Example A=[1 2 3; 4 5 6; 7 8 9]; B=A'; B(: , 3: -1: 1) ans = 7 8 9 4 5 6 A= 1 4 7 2 5 8 3 6 9 1 2 3 軟體實作與計算實驗 7

Plot a function x=linspace(-5, 5); plot(x, cos(x)); 軟體實作與計算實驗 8

Plot a function x=linspace(-5, 5); plot(x, cos(x)); 軟體實作與計算實驗 8

rand(m, n) Create a matrix n n m rows n columns Elements are a

rand(m, n) Create a matrix n n m rows n columns Elements are a sample from a uniform distribution within [0 1] 軟體實作與計算實驗 9

A sample from a unit square X=rand(2, 800); plot(X(1, : ), X(2, : ),

A sample from a unit square X=rand(2, 800); plot(X(1, : ), X(2, : ), '. '); 軟體實作與計算實驗 10

Noisy function x=linspace(-5, 5); n=length(x); plot(x, cos(x)+(rand(1, n)-0. 5)*0. 3) 軟體實作與計算實驗 11

Noisy function x=linspace(-5, 5); n=length(x); plot(x, cos(x)+(rand(1, n)-0. 5)*0. 3) 軟體實作與計算實驗 11

ones(m, n) Create a matrix n n m rows n column All ones in

ones(m, n) Create a matrix n n m rows n column All ones in the created matrix 軟體實作與計算實驗 12

zeros(m, n) Create a matrix n n m rows n columns All zeros in

zeros(m, n) Create a matrix n n m rows n columns All zeros in the created matrix 軟體實作與計算實驗 13

1 D series Sounds Series Strings 軟體實作與計算實驗 14

1 D series Sounds Series Strings 軟體實作與計算實驗 14

Read sounds [s 1, fs]=wavread('ic_a 1. wav'); plot(1: length(s 1), s 1) ic_a 1.

Read sounds [s 1, fs]=wavread('ic_a 1. wav'); plot(1: length(s 1), s 1) ic_a 1. wav 軟體實作與計算實驗 15

Play sounds [s 1, fs]=wavread('ic_a 1. wav'); wavplay(s 1, fs); • s 1 is

Play sounds [s 1, fs]=wavread('ic_a 1. wav'); wavplay(s 1, fs); • s 1 is a vector that represents the recorded sound • fs denotes the frequency 軟體實作與計算實驗 16

Slow down [s 1, fs]=wavread('ic_a 1. wav'); wavplay(s 1, fs*8/10); 軟體實作與計算實驗 17

Slow down [s 1, fs]=wavread('ic_a 1. wav'); wavplay(s 1, fs*8/10); 軟體實作與計算實驗 17

Play quicker [s 1, fs]=wavread('ic_a 1. wav'); wavplay(s 1, fs*15/10); 軟體實作與計算實驗 18

Play quicker [s 1, fs]=wavread('ic_a 1. wav'); wavplay(s 1, fs*15/10); 軟體實作與計算實驗 18

Digital voice recorder Sing a song Your recorder may produce a sound file in

Digital voice recorder Sing a song Your recorder may produce a sound file in WMA format WS_20022. WMA 軟體實作與計算實驗 19

Audio converter software Convert WMA file to WAV file MP 3 -OGG-WAV-WMA Converter WS_20022.

Audio converter software Convert WMA file to WAV file MP 3 -OGG-WAV-WMA Converter WS_20022. wav 軟體實作與計算實驗 20

Read and play a WAV file [s 1, fs]=wavread('ws_20022. wav'); wavplay(s 1, fs) 軟體實作與計算實驗

Read and play a WAV file [s 1, fs]=wavread('ws_20022. wav'); wavplay(s 1, fs) 軟體實作與計算實驗 21

Display sounds [s 1, fs]=wavread('ws_20022. wav'); subplot(2, 1, 1) plot(1: length(s 1), s 1(:

Display sounds [s 1, fs]=wavread('ws_20022. wav'); subplot(2, 1, 1) plot(1: length(s 1), s 1(: , 1)) subplot(2, 1, 2) plot(1: length(s 1), s 1(: , 2)) 軟體實作與計算實驗 22

Segmentation s 2=s 1(310000: length(s 1)); wavplay(s 2, fs); plot(1: length(s 2), s 2);

Segmentation s 2=s 1(310000: length(s 1)); wavplay(s 2, fs); plot(1: length(s 2), s 2); • Form a segment that consists of signals after 310000 軟體實作與計算實驗 23

indexing s 1(310000: length(s 1)) Get elements from 310000 to the end of s

indexing s 1(310000: length(s 1)) Get elements from 310000 to the end of s 1(length(s 1): -1: 310000) Reverse the segment 軟體實作與計算實驗 24

sun spot data Data sunspot_train sunspot_test_1. mat sunspot_test_2. mat 軟體實作與計算實驗 25

sun spot data Data sunspot_train sunspot_test_1. mat sunspot_test_2. mat 軟體實作與計算實驗 25

time series: sun spot Source codes load sunspot_train. mat; subplot(3, 1, 1); plot(1712: 1920,

time series: sun spot Source codes load sunspot_train. mat; subplot(3, 1, 1); plot(1712: 1920, Y); load sunspot_test_1. mat; subplot(3, 1, 2); plot(1921: 1955, Y); load sunspot_test_2. mat; subplot(3, 1, 3); plot(1956: 1979, Y); 軟體實作與計算實驗 26

load sunspot_train. mat; Load an MAT file An MAT file stores variables of work

load sunspot_train. mat; Load an MAT file An MAT file stores variables of work space 軟體實作與計算實驗 27

subplot(m, n, k) Create a figure that is composed of m rows and n

subplot(m, n, k) Create a figure that is composed of m rows and n columns of subplots Select the k-th subplot Column-major order 軟體實作與計算實驗 28

Create strings name = ['Thomas' ' R. ' 'Lee'] name = strcat('Thomas', ' R.

Create strings name = ['Thomas' ' R. ' 'Lee'] name = strcat('Thomas', ' R. ', ' Lee') 軟體實作與計算實驗 29

A vertical array of strings Create a vertical array of strings. C = strvcat('Hello',

A vertical array of strings Create a vertical array of strings. C = strvcat('Hello', 'Yes', 'No', 'Goodbye') C= Hello Yes No Goodbye • C is a matrix • C(i, : ) gets the i-th row 軟體實作與計算實驗 30

Cell array of strings Create a cell array of strings. S = {'Hello' 'Yes'

Cell array of strings Create a cell array of strings. S = {'Hello' 'Yes' 'No' 'Goodbye'} S= 'Hello' 'Yes' 'No' 軟體實作與計算實驗 'Goodbye' 31

Cell array of strings S = {'Hello' 'Yes' 'No' 'Goodbye'} size(S) n n Return

Cell array of strings S = {'Hello' 'Yes' 'No' 'Goodbye'} size(S) n n Return the size of the cell array Each cell in S contains a string S(1, 2) n Specify the string stored at the second column 軟體實作與計算實驗 32

Example T={'Mon' 'Tue' 'Wen' 'Thu'} S = {'Hello' 'Yes' 'No' 'Goodbye'} ST=[S; T] 軟體實作與計算實驗

Example T={'Mon' 'Tue' 'Wen' 'Thu'} S = {'Hello' 'Yes' 'No' 'Goodbye'} ST=[S; T] 軟體實作與計算實驗 33

Color images A=imread('CMW 2. jpg'); image(A); CMW 2. jpg 取自網路 軟體實作與計算實驗 34

Color images A=imread('CMW 2. jpg'); image(A); CMW 2. jpg 取自網路 軟體實作與計算實驗 34

rgb 2 gray A=imread('CMW 2. jpg'); image(A); IG=rgb 2 gray(A); imshow(IG); imwrite(IG, 'cmw 2.

rgb 2 gray A=imread('CMW 2. jpg'); image(A); IG=rgb 2 gray(A); imshow(IG); imwrite(IG, 'cmw 2. bmp', 'bmp') 軟體實作與計算實驗 35

Gray image I=imread('cmw 1. bmp'); imshow(I) 軟體實作與計算實驗 36

Gray image I=imread('cmw 1. bmp'); imshow(I) 軟體實作與計算實驗 36

Size of an image >> size(I) ans = 235 275 軟體實作與計算實驗 37

Size of an image >> size(I) ans = 235 275 軟體實作與計算實驗 37

Uint 8 to double I=imread('cmw 1. bmp'); imshow(I) J=double(I) • I is a matrix

Uint 8 to double I=imread('cmw 1. bmp'); imshow(I) J=double(I) • I is a matrix with elements(uint 8) • J is a matrix with double elements belonging {0, …, 255} 軟體實作與計算實驗 38

Double to uint 8 I=imread('cmw 1. bmp'); J=double(I) Imshow(uint 8(J)) 軟體實作與計算實驗 39

Double to uint 8 I=imread('cmw 1. bmp'); J=double(I) Imshow(uint 8(J)) 軟體實作與計算實驗 39

Reshape to a vector I=imread('cmw 1. bmp'); [m, n]=size(I) J=double(I); Jv=reshape(I, 1, m*n); 軟體實作與計算實驗

Reshape to a vector I=imread('cmw 1. bmp'); [m, n]=size(I) J=double(I); Jv=reshape(I, 1, m*n); 軟體實作與計算實驗 40

reshape I=imread('cmw 1. bmp'); [m, n]=size(I) J=double(I); Jv=reshape(I, 1, m*n); v. J=reshape(Jv, m, n);

reshape I=imread('cmw 1. bmp'); [m, n]=size(I) J=double(I); Jv=reshape(I, 1, m*n); v. J=reshape(Jv, m, n); imshow(uint 8(v. J)); 軟體實作與計算實驗 41

Horizontal mirror transpose 軟體實作與計算實驗 Vertical mirror 42

Horizontal mirror transpose 軟體實作與計算實驗 Vertical mirror 42