250 likes | 374 Vues
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,
E N D
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, • Plotsimple graph using arrays, • Manipulatesound using array operations
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
Size of Arrays • whos: prints its size, class, number of bytes • size: print its number of rows and columns • length: max(size(v)) s=size(a) 2 3 a [m,n]=size(a) 3 2 m n 1 2 3 m=size(a,1) 2 4 5 6 n=size(a,2) 3 l=length(a) 3 e=numel(a) 6
Plotting Arrays • plot, stem, bar, ... 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)
Complex Array Element • MATLAB understands i or j as [ 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 ] MATLAB is smart! 0 + 1.0000i 8.0000 1.0000 + 1.0000i 8 a(1) a(2) a(3) a(4)
Array Addressing 1/2 • Array index starts from 1. Array Index : 1 2 3 4 5 6 0 a=[ 0:0.2:1]; 0.2 0.4 0.6 0.8 1.0 0 b=a(1:3); 0.2 0.4 0 b=a(1:2:5); 0.4 0.8 b=a(3:end); 0.6 0.8 1.0 0.4 b=a(end:-1:1); 1.0 0.8 0.6 0.4 0.2 0 b=a([3 1 4 2 6 5]); 0.4 0 0.6 0.2 1.0 0.8
Array Addressing 2/2 • Index should be integer type or an integer value 0 a=[ 0:0.2:1]; 0.2 0.4 0.6 0.8 1.0 b=a(8); b=a(1.2); b=a(ceil(2.3*2)); 0.8 b=a(int16(2.3)); 0.2 b=a(int16([2.3 3.2]); 0.4 0.2
Creating Arrays • Range: start : step : end 3.0000 a=[ 3 pi log(2) ]; 3.1416 0.6931 3 a=3:6; 4 5 6 8 a=8:-1:6; 7 6 5 0 a=[0:2:6] * 0.1; 0.2 0.4 0.6 1.00 a=linspace(1,2,5); 1.25 2.00 1.50 1.75 1 a=logpace(0,4,5); 10 10000 100 1000
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 .
Plotting Sound Waveform • Read a sound file using wavread( ) and plot it. 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
Orientation • Row or Column Vector • Transpose operator: ' 3 a=[ 3;5;2 ]; 3 b=a'; 5 2 Transpose 5 2 a=[1 2; 4 5] b=a'; 1 2 1 4 4 5 2 5
Complex Transpose • Hermitian – Complex conjugate transpose: ' • Just transpose: .' a=[1:3]'; 1 b=complex(a,a); 1+1i 2 2+2i 3 3+3i c=b'; 1-1i 2-2i 3-3i c=b.'; 1+1i 2+2i 3+3i
Scalar – Array Math Operations • Scalar operations affect all the elements in the array. a=[1 2; 4 5] b=a+2 1 3 2 4 4 6 5 7 b=2*a-1 b=a/2+1 1 1.5 3 2.0 7 3.0 9 3.5
Array – Array Math Operations 1/2 • Standard matrix operations apply. a=[1 2; 4 5] 1 2 x=a+b 2 4 4 5 5 6 b=[0 0; 1 1] 1 2 x=a.*b 1 4 1 1 4 5 x=a*b x=a/b 3 1 4 0 9 1 13 3
Array – Array Math Operations 1/2 • Element wise operations: .*, ./, .^ a=[1 2; 4 5] 1 2 4 5 x=a.^2 x=2.^a 1 4 2 4 16 16 25 32 x=a^2 x=a.^a 9 1 12 4 24 256 33 3125
Ones and Zeros • Useful for creating basic arrays for operations a=ones(2) b=ones(2,1) 1 1 1 1 1 1 c=zeros(3) d=size(c) 0 0 3 3 0 0 0 0 m=2*ones(2) 2 2 0 0 0 2 2
Special Matrices • Identity matrix: eye(n) • Magic square: magic(n) b=eye(3,2) 1 0 a=eye(2) 1 0 0 1 0 1 0 0 c=rand(2) d=magic(3) 8 1 6 0.4103 0.0579 3 5 7 0.8936 0.3529 4 9 2 Uniform random variable between 0 and 1
Diagonal Matrix • diag(v,n) a=1:3 b=diag(a) 1 1 0 0 2 3 0 2 0 0 0 3 b=diag(a,1) b=diag(a,-1) 0 1 0 0 0 0 0 0 2 1 0 0 0 0 0 0 2 0
Array with the Same Elements • repmat( ) is the fastest. SLOW d=3.14 a = d * ones(5,3) 3.14 3.14 3.14 a = d + zeros(5,3) 3.14 3.14 3.14 3.14 3.14 3.14 a = d( ones(5,3) ) 3.14 3.14 3.14 a = repmat(d,5,3) 3.14 3.14 3.14 FAST
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) a= [1 2 3; 4 5 6; 7 8 9] b= a(1,:) 1 2 3 4 5 6 7 8 9 c= a(:,2:end)
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. tha---n-- - k- you-- tha---n-- - k- you-- tha---n-- - k- you-- ref: two.wav
Solution 2 • Script • Sound Waveform 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