Download presentation
Presentation is loading. Please wait.
Published byNigel McGee Modified over 9 years ago
1
Handling Arrays 1/2 Numerical Computing with. MATLAB for Scientists and Engineers
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 2
3
Simple Array Array: Collection of scalar values Vector: n x 1 or 1 x n Matrix: n x m 3 >> 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 >> 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
4
Size of Arrays whos: prints its size, class, number of bytes size: print its number of rows and columns length: max(size(v)) 4 1 1 2 2 3 3 4 4 5 5 6 6 a a s=size(a) 2 2 3 3 [m,n]=size(a) 2 2 3 3 mn m=size(a,1) 2 2 3 3 n=size(a,2) 3 3 l=length(a) 6 6 e=numel(a)
5
Plotting Arrays plot, stem, bar,... 5 matrix_plot.m % Plotting vector v = [1 3 2 4]; figure(1), plot(v,'r-'); figure(2), plot(v,'bs'); figure(3), stem(v); figure(4), bar(v) % plotting matrix M = [1 3 2 4 8 3 5; 2 6 1 2 4 5 2]'; figure(5), plot(M) figure(6), bar(M) % Plotting vector v = [1 3 2 4]; figure(1), plot(v,'r-'); figure(2), plot(v,'bs'); figure(3), stem(v); figure(4), bar(v) % plotting matrix M = [1 3 2 4 8 3 5; 2 6 1 2 4 5 2]'; figure(5), plot(M) figure(6), bar(M)
6
Complex Array Element MATLAB understands i or j as 6 [ i 8 1+i sin(0.1*pi)] 0 + 1.0000i 8.0000 1.0000 + 1.0000i 0.3090 a=[ i 8 1 + i 2 * 4 ] 0 + 1.0000i 8.0000 1.0000 + 1.0000i 8 8 a(1) a(2) a(3) a(4) MATLAB is smart!
7
Array Addressing 1/2 Array index starts from 1. 7 a=[ 0:0.2:1]; 0 0 b=a(1:3); 0.2 0.4 0.6 0.8 1.0 0 0 0.2 0.4 b=a(1:2:5); 0 0 0.4 0.8 123456Array Index : b=a(3:end); 0.4 0.8 0.6 1.0 b=a(end:-1:1); 0.4 0.8 0.6 1.0 0 0 0.2 b=a([3 1 4 2 6 5]); 0.4 0.8 0.6 1.0 0 0 0.2
8
Array Addressing 2/2 Index should be integer type or an integer value 8 a=[ 0:0.2:1]; 0 0 b=a(1.2); 0.2 0.4 0.6 0.8 1.0 b=a(8); b=a(ceil(2.3*2)); b=a(int16(2.3)); b=a(int16([2.3 3.2]); 0.8 0.2 0.4
9
Creating Arrays Range: start : step : end 9 a=[ 3 pi log(2) ]; 3.0000 3.1416 0.6931 a=3:6; 3 3 4 4 5 5 6 6 a=8:-1:6; 8 8 7 7 6 6 5 5 a=[0:2:6] * 0.1; 0 0 0.2 0.4 0.6 a=linspace(1,2,5); 1.00 1.25 1.50 1.75 2.00 a=logpace(0,4,5); 1 1 10 100 1000 10000
10
Exercise 1 - Creating Arrays Create a row vector in which the first element is 5, and the last element is 41, with an increment of 3. Create a column vector that has the elements: 3, ln(2), 0.34, and. 10
11
Plotting Sound Waveform Read a sound file using wavread( ) and plot it. 11 waveform_plot.m [s Fs] = wavread('thankyou.wav'); Ts = 1/Fs; % time between two adjacent samples N = length(s); t = (0:N-1)*Ts; % generate time values figure(1), plot(t,s); soundsc(s,Fs) % play the sound [s Fs] = wavread('thankyou.wav'); Ts = 1/Fs; % time between two adjacent samples N = length(s); t = (0:N-1)*Ts; % generate time values figure(1), plot(t,s); soundsc(s,Fs) % play the sound
12
Orientation Row or Column Vector Transpose operator: ' 12 a=[ 3;5;2 ]; a=[1 2; 4 5] 3 3 5 5 2 2 1 1 2 2 4 4 5 5 b=a'; 3 3 2 2 5 5 Transpose b=a'; 1 1 4 4 2 2 5 5
13
Complex Transpose Hermitian – Complex conjugate transpose: ' Just transpose:.' 13 a=[1:3]'; c=b'; b=complex(a,a); 1 1 3 3 2 2 1+1i 3+3i 2+2i 1-1i 2-2i 3-3i c=b.'; 1+1i 2+2i 3+3i
14
Scalar – Array Math Operations Scalar operations affect all the elements in the array. 14 a=[1 2; 4 5] 1 1 2 2 4 4 5 5 b=a+2 3 3 4 4 6 6 7 7 b=2*a-1 1 1 3 3 7 7 9 9 b=a/2+1 1.5 2.0 3.0 3.5
15
Array – Array Math Operations 1/2 Standard matrix operations apply. 15 a=[1 2; 4 5] 1 1 2 2 4 4 5 5 b=[0 0; 1 1] 1 1 2 2 1 1 1 1 x=a+b 2 2 4 4 5 5 6 6 x=a.*b 1 1 4 4 4 4 5 5 x=a*b 3 3 4 4 9 9 13 x=a/b 1 1 0 0 1 1 3 3
16
Array – Array Math Operations 1/2 Element wise operations:.*,./,.^ 16 a=[1 2; 4 5] 1 1 2 2 4 4 5 5 x=a.^2 1 1 4 4 16 25 x=2.^a 2 2 4 4 16 32 x=a^2 9 9 12 24 33 x=a.^a 1 1 4 4 256 3125
17
Ones and Zeros Useful for creating basic arrays for operations 17 a=ones(2) 1 1 1 1 1 1 1 1 c=zeros(3) 0 0 0 0 d=size(c) 3 3 3 3 b=ones(2,1) 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 m=2*ones(2) 2 2 2 2 2 2 2 2
18
Special Matrices Identity matrix: eye(n) Magic square: magic(n) 18 a=eye(2) 1 1 0 0 0 0 1 1 c=rand(2) 0.4103 b=eye(3,2) 1 1 0 0 0 0 0 0 1 1 0 0 0.0579 0.8936 0.3529 Uniform random variable between 0 and 1 d=magic(3) 8 8 3 3 4 4 1 1 5 5 9 9 6 6 7 7 2 2
19
Diagonal Matrix diag(v,n) 19 a=1:3 1 1 2 2 3 3 b=diag(a,1) 0 0 1 1 0 0 0 0 0 0 2 2 0 0 0 0 0 0 b=diag(a) 1 1 0 0 0 0 0 0 2 2 0 0 0 0 0 0 3 3 b=diag(a,-1) 0 0 0 0 0 0 1 1 0 0 0 0 0 0 2 2 0 0
20
Array with the Same Elements repmat( ) is the fastest. 20 3.14 a = d * ones(5,3) 3.14 SLOW FAST d=3.14 a = d + zeros(5,3) a = d( ones(5,3) ) a = repmat(d,5,3)
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) 21 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 a= [1 2 3; 4 5 6; 7 8 9] b= a(1,:) c= a(:,2:end)
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. 22 tha---n-- - k- you-- ref: two.wav
23
Solution 2 Script Sound Waveform 23 thankou_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
24
Notes 24
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.