handling arrays 1/2

24
Handling Arrays 1/2 Numerical Computing with . MATLAB for Scientists and Engineers

Upload: thom

Post on 21-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Handling Arrays 1/2. Numerical Computing with . MATLAB for Scientists and Engineers. You will be able to. Create vectors and matrices , Perform math operations on matrices , Extract part of a matrix, Plot simple graph using arrays, - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Handling Arrays 1/2

Handling Arrays 1/2

Numerical Computingwith .

MATLAB for Scientists and Engineers

Page 2: Handling Arrays 1/2

2

You will be able to

Create vectors and matrices, Perform math operations on matrices, Extract part of a matrix, Plot simple graph using arrays, Manipulate sound using array operations

Page 3: Handling Arrays 1/2

3

Simple Array

Array: Collection of scalar values Vector: n x 1 or 1 x n Matrix: n x m>> m = [ 1 3 2 4]m = 1 3 2 4>> v = [ 1; 2; 3; 4]v = 1 2 3 4>> M = [ 1 2; 3 4]M = 1 2 3 4

row vector

column vector

matrix

Page 4: Handling Arrays 1/2

4

Size of Arrays

whos: prints its size, class, number of bytes size: print its number of rows and columns length: max(size(v))

11 22 33

44 55 66

aa

s=size(a)s=size(a) 22 33

[m,n]=size(a)[m,n]=size(a) 22 33m n

m=size(a,1)m=size(a,1) 22

33n=size(a,2)n=size(a,2)

33l=length(a)l=length(a)

66e=numel(a)e=numel(a)

Page 5: Handling Arrays 1/2

5

Plotting Arrays

plot, stem, bar, ...matrix_plot.mmatrix_plot.m

%% Plotting vectorv = [1 3 2 4];figure(1), plot(v,'r-');figure(2), plot(v,'bs');figure(3), stem(v);figure(4), bar(v)%% plotting matrixM = [1 3 2 4 8 3 5; 2 6 1 2 4 5 2]';figure(5), plot(M)figure(6), bar(M)

%% Plotting vectorv = [1 3 2 4];figure(1), plot(v,'r-');figure(2), plot(v,'bs');figure(3), stem(v);figure(4), bar(v)%% plotting matrixM = [1 3 2 4 8 3 5; 2 6 1 2 4 5 2]';figure(5), plot(M)figure(6), bar(M)

1 1.5 2 2.5 3 3.5 41

1.5

2

2.5

3

3.5

4

1 1.5 2 2.5 3 3.5 41

1.5

2

2.5

3

3.5

4

1 1.5 2 2.5 3 3.5 40

1

2

3

4

1 2 3 40

1

2

3

4

1 2 3 4 5 6 70

2

4

6

8

1 2 3 4 5 6 70

2

4

6

8

Page 6: Handling Arrays 1/2

6

Complex Array Element

MATLAB understands i or j as 1

[ i 8 1+i sin(0.1*pi)][ i 8 1+i sin(0.1*pi)]

0 + 1.0000i0 + 1.0000i 8.00008.0000 1.0000 + 1.0000i1.0000 + 1.0000i 0.3090 0.3090

a=[ i 8 1 + i 2 * 4 ]a=[ i 8 1 + i 2 * 4 ]

0 + 1.0000i0 + 1.0000i 8.00008.0000 1.0000 + 1.0000i1.0000 + 1.0000i 8 8

a(1)a(1) a(2)a(2) a(3)a(3) a(4)a(4)

MATLAB is smart!MATLAB is smart!

Page 7: Handling Arrays 1/2

7

Array Addressing 1/2

Array index starts from 1.

a=[ 0:0.2:1];a=[ 0:0.2:1]; 00

b=a(1:3);b=a(1:3);

0.20.2 0.40.4 0.60.6 0.80.8 1.01.0

00 0.20.2 0.40.4

b=a(1:2:5);b=a(1:2:5); 00 0.40.4 0.80.8

1 2 3 4 5 6Array Index :

b=a(3:end);b=a(3:end); 0.40.4 0.80.80.60.6 1.01.0

b=a(end:-1:1);b=a(end:-1:1); 0.40.40.80.8 0.60.61.01.0 000.20.2

b=a([3 1 4 2 6 5]);b=a([3 1 4 2 6 5]); 0.40.4 0.80.80.60.6 1.01.000 0.20.2

Page 8: Handling Arrays 1/2

8

Array Addressing 2/2

Index should be integer type or an integer value

a=[ 0:0.2:1];a=[ 0:0.2:1]; 00

b=a(1.2);b=a(1.2);

0.20.2 0.40.4 0.60.6 0.80.8 1.01.0

b=a(8);b=a(8);

b=a(ceil(2.3*2));b=a(ceil(2.3*2));

b=a(int16(2.3));b=a(int16(2.3));

b=a(int16([2.3 3.2]);b=a(int16([2.3 3.2]);

0.80.8

0.20.2

0.20.2 0.40.4

Page 9: Handling Arrays 1/2

9

Creating Arrays

Range: start : step : end

a=[ 3 pi log(2) ];a=[ 3 pi log(2) ]; 3.00003.0000 3.14163.1416 0.69310.6931

a=3:6;a=3:6; 33 44 55 66

a=8:-1:6;a=8:-1:6; 88 77 66 55

a=[0:2:6] * 0.1;a=[0:2:6] * 0.1; 00 0.20.2 0.40.4 0.60.6

a=linspace(1,2,5);a=linspace(1,2,5); 1.001.00 1.251.25 1.501.50 1.751.75 2.002.00

a=logpace(0,4,5);a=logpace(0,4,5); 11 1010 100100 10001000 1000010000

010 110 210 310 410

Page 10: Handling Arrays 1/2

10

Exercise 1 - Creating Arrays

Create a row vector in which the first element is 5, and the last element is 41, with an in-crement of 3.

Create a column vector that has the ele-ments: 3, ln(2), 0.34, and . sin(0.1 )

Page 11: Handling Arrays 1/2

11

Plotting Sound Waveform

Read a sound file using wavread( ) and plot it.

waveform_plot.mwaveform_plot.m

[s Fs] = wavread('thankyou.wav');Ts = 1/Fs; % time between two adjacent samplesN = length(s);t = (0:N-1)*Ts; % generate time valuesfigure(1), plot(t,s);soundsc(s,Fs) % play the sound

[s Fs] = wavread('thankyou.wav');Ts = 1/Fs; % time between two adjacent samplesN = length(s);t = (0:N-1)*Ts; % generate time valuesfigure(1), plot(t,s);soundsc(s,Fs) % play the sound

0 0.2 0.4 0.6 0.8 1 1.2 1.4-0.2

-0.1

0

0.1

0.2

Page 12: Handling Arrays 1/2

12

Orientation

Row or Column Vector Transpose operator: '

a=[ 3;5;2 ];a=[ 3;5;2 ];

a=[1 2; 4 5]a=[1 2; 4 5]

33 55 22

11 22

44 55

b=a';b=a';33

22

55 Transpose

b=a';b=a'; 11 44

22 55

Page 13: Handling Arrays 1/2

13

Complex Transpose

Hermitian – Complex conjugate transpose: ' Just transpose: .'

a=[1:3]';a=[1:3]';

c=b';c=b';

b=complex(a,a);b=complex(a,a);11

33

22

1+1i1+1i

3+3i3+3i

2+2i2+2i

1-1i1-1i 2-2i2-2i 3-3i3-3i

c=b.';c=b.'; 1+1i1+1i 2+2i2+2i 3+3i3+3i

Page 14: Handling Arrays 1/2

14

Scalar – Array Math Operations

Scalar operations affect all the elements in the array.

a=[1 2; 4 5]a=[1 2; 4 5] 11 22

44 55

b=a+2b=a+2 33 44

66 77

b=2*a-1b=2*a-1 11 33

77 99

b=a/2+1b=a/2+1 1.51.5 2.02.0

3.03.0 3.53.5

Page 15: Handling Arrays 1/2

15

Array – Array Math Operations 1/2

Standard matrix operations apply.

a=[1 2; 4 5]a=[1 2; 4 5] 11 22

44 55

b=[0 0; 1 1]b=[0 0; 1 1] 11 22

11 11

x=a+bx=a+b 22 44

55 66

x=a.*bx=a.*b 11 44

44 55

x=a*bx=a*b 33 44

99 1313

x=a/bx=a/b 11 00

11 33

Page 16: Handling Arrays 1/2

16

Array – Array Math Operations 1/2

Element wise operations: .*, ./, .^

a=[1 2; 4 5]a=[1 2; 4 5] 11 22

44 55

x=a.^2x=a.^2 11 44

1616 2525

x=2.^ax=2.^a 22 44

1616 3232

x=a^2x=a^2 99 1212

2424 3333

x=a.^ax=a.^a 11 44

256256 31253125

Page 17: Handling Arrays 1/2

17

Ones and Zeros

Useful for creating basic arrays for operations

a=ones(2)a=ones(2) 11 11

11 11

c=zeros(3)c=zeros(3) 00

00

d=size(c)d=size(c) 33 33

b=ones(2,1)b=ones(2,1) 11

11

00

00

00

00

00

00

00m=2*ones(2)m=2*ones(2) 22 22

22 22

Page 18: Handling Arrays 1/2

18

Special Matrices

Identity matrix: eye(n) Magic square: magic(n)

a=eye(2)a=eye(2) 11 00

00 11

c=rand(2)c=rand(2) 0.41030.4103

b=eye(3,2)b=eye(3,2) 11

00

00

00

11

00

0.05790.0579

0.89360.8936 0.35290.3529

Uniform random variablebetween 0 and 1

d=magic(3)d=magic(3) 88

33

44

11

55

99

66

77

22

Page 19: Handling Arrays 1/2

19

Diagonal Matrix

diag(v,n)

a=1:3a=1:3 11 22 33

b=diag(a,1)b=diag(a,1) 00 11 00

00 00 22

00 00 00

b=diag(a)b=diag(a) 11 00 00

00 22 00

00 00 33

b=diag(a,-1)b=diag(a,-1) 00 00 00

11 00 00

00 22 00

Page 20: Handling Arrays 1/2

20

Array with the Same Elements

repmat( ) is the fastest.

3.143.14

a = d * ones(5,3)a = d * ones(5,3)

3.143.14 3.143.14

3.143.14 3.143.14 3.143.14

3.143.14 3.143.14 3.143.14

3.143.14 3.143.14 3.143.14

3.143.14 3.143.14 3.143.14

SLOW

FAST

d=3.14 d=3.14

a = d + zeros(5,3)a = d + zeros(5,3)

a = d( ones(5,3) )a = d( ones(5,3) )

a = repmat(d,5,3)a = repmat(d,5,3)

Page 21: Handling Arrays 1/2

21

Getting Sub-matrices

Getting rows n-th row: M( n, : ) m~n-th row: M( m:n,:)

Getting columns r-th column: M( :, r ) r~s-th cols: M( :,r:s)

11 22 33

44 55 66

77 88 99

a= [1 2 3; 4 5 6; 7 8 9]a= [1 2 3; 4 5 6; 7 8 9]

b= a(1,:)b= a(1,:)

c= a(:,2:end)c= a(:,2:end)

Page 22: Handling Arrays 1/2

22

Exercise 2 - Sound Editing

Extract 'than-' female voice and '-kyou' male voice from 'thankyou.wav'. Concatenate them to forge a new 'thank-you' voice. Play the sound and plot the waveform.

0 0.2 0.4 0.6 0.8 1 1.2 1.4-0.2

-0.1

0

0.1

0.2

0 0.1 0.2 0.3 0.4 0.5-0.2

0

0.2

tha---n-- - k- you-- tha---n-- - k- you--

tha---n-- - k- you--

ref: two.wavref: two.wav

Page 23: Handling Arrays 1/2

23

Solution 2

Script

Sound Waveform

thankou_twovoices.mthankou_twovoices.m

[s Fs] = wavread('thankyou.wav');figure(1), plot(s);soundsc(s,Fs) % play the sound..

soundsc(two,Fs) % play the forged sound

[s Fs] = wavread('thankyou.wav');figure(1), plot(s);soundsc(s,Fs) % play the sound..

soundsc(two,Fs) % play the forged sound

Page 24: Handling Arrays 1/2

24

Notes