20 - Subarrays and Vectorization

# 20 - Subarrays and Vectorization - Engineering 101 Quote of...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day To conceal ignorance is to increase it. An honest confession of it, however, gives ground for the hope that it will diminish some day or the other. - Mahatma Gandhi Subarrays and Vectorization Using Subarrays You can use pieces of arrays just as you would arrays. For example if you have an array x = [ 1 7 4 1 13 ] x(3) is 4 x([1 3 5]) is [1 4 13] Using Subarrays In addition you can use : notation to select elements. end denotes the last element. a = [ 1 5 8 10 3 21 9 ] b = a(1:2:end) b=[1839] A colon on its own selects all values x = [ 7 8 9 ; 1 2 3; -1 -2 -3 ] y = x( :, 1:2 ) y= 7 1 -1 8 2 -2 Using Subarrays You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] Using Subarrays You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr([1 3],1:2) = [ -4 -3; -2 -1 ] 1 5 9 2 6 10 3 7 11 4 8 12 1 5 9 2 6 10 3 7 11 4 8 12 1 Using Subarrays You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr([1 3],1:2) = [ -4 -3; -2 -1 ] -4 5 -2 -3 6 -1 3 7 11 4 8 12 Using Subarrays You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr( : , [2, 4] ) = 0 1 5 9 2 6 10 3 7 11 4 8 12 Using Subarrays You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] arr( : , [2, 4] ) = 0 Exercise Which DOES NOT create the following matrix? -1 -1 0 0 0 0 -1 -1 0 0 0 0 0 0 3 3 0 0 0 0 3 3 0 0 0 0 3 3 0 0 0 0 0 0 -1 -1 0 0 0 0 -1 -1 1 1 5 9 0 0 0 3 7 11 0 0 0 2 3 Which function swaps the elements in the even numbered positions of a vector with those in the odd numbered positions? 1 Vectorizing It is possible to use for-loops to perform formathematical operations on arrays. But taking advantage of MATLAB's natural ability to work with arrays and vectors greatly speeds up code. These expressions are almost always faster than for or while loops 2 3 4 2 Vectorizing Expressions When we create a matrix by using a logical operator that array is a logical array x = [ 1 7 10; 9 4 2 ]; y = x > 5; We can also make any array logical by using the logical function z = logical(x); Vectorizing Expressions A logical array can be used to determine when an operation will be carried out (matrix addressing). Suppose we wanted to subtract 2 from all values greater than 5 x = [ 1 7 10; 9 4 2 ]; y = x > 5; x= 1 5 8 7 10 x(y) = x(y)-2; x(y)9 742 y= 011 100 Example Which function subtracts 1 from every number that is divisible by s in matrix A? 1 2 Which MATLAB function is equivalent to: void shuffle (vector <int> A, int max, vector <int> & B){ for(int i=0, B.clear( ); i< A.size( ); i++) if (A[ i ] <= max) B.push_back(A[ i ]); return; } 3 4 sine_plot.m Vectorizing Expressions This method can also be used to avoid if else expressions What if we want to create a vector that has the values of sin(t) if sin(t) is positive, otherwise it should be 0? x = 0:pi/50.0:4.0*pi; f = sin(x); select = f < 0; f(select) = 0; plot(x, f); Example Which is an equivalent vectorized expression? for i = 1:100 a(i) = exp(-0.2 * i); exp(end 3 Example Which is an equivalent vectorized expression? % rand fills a matrix with random numbers % otherwise the syntax is like zeros g=rand(1, 10); for i = 1:2:length(g)-1 1:2:length(g)g(i) = g(i+1); end Vectorization is Critical to Effective MATLAB Programming The use of vectorization and logical arrays dramatically speeds up performance. Therefore, the effective use of these tools is essential if your MATLAB programs are to be efficient. MATLAB Programming Write a short program with no loops that will create an 11 x 11 matrix that contains the distance of each point from the location 6,6. Unless the distance is less than 2, in which case it should be replaced by 2. e.g. M(1,1) = sqrt(50.0); (1,1) M(5,6) = 1 2; (5,6) (6,6) MATLAB Programming v= -5:5; -5 -4 -3 -2 -1 0 1 2 3 4 5 MATLAB Programming col -5 -4 -3 -2 -1 0 -5 -4 -3 -2 -1 0 -5 -4 -3 -2 -1 0 -5 -4 -3 -2 -1 0 -5 -4 -3 -2 -1 0 -5 -4 -3 -2 -1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0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 v= -5:5; col = [v;v;v;v;v;v;v;v;v;v;v]; row = col'; M = sqrt(row.^2 + col.^2); low = M<2.0; 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] M= [ 7.0711 6.4031 5.8310 5.3852 5.0990 5.0000 5.0990 5.3852 5.8310 6.4031 7.0711 6.4031 5.6569 5.0000 4.4721 4.1231 4.0000 4.1231 4.4721 5.0000 5.6569 6.4031 5.8310 5.0000 4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426 5.0000 5.8310 5.3852 4.4721 3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056 4.4721 5.3852 5.0990 4.1231 3.1623 2.2361 1.4142 1.0000 1.4142 2.2361 3.1623 4.1231 5.0990 5.0000 4.0000 3.0000 2.0000 1.0000 0 1.0000 2.0000 3.0000 4.0000 5.0000 5.0990 4.1231 3.1623 2.2361 1.4142 1.0000 1.4142 2.2361 3.1623 4.1231 5.0990 5.3852 4.4721 3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056 4.4721 5.3852 5.8310 5.0000 4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426 5.0000 5.8310 6.4031 5.6569 5.0000 4.4721 4.1231 4.0000 4.1231 4.4721 5.0000 5.6569 6.4031 7.0711 6.4031 5.8310 5.3852 5.0990 5.0000 5.0990 5.3852 5.8310 6.4031 7.0711 ] MATLAB Programming v= -5:5; col = [v;v;v;v;v;v;v;v;v;v;v]; row = col'; M = sqrt(row.^2 + col.^2); low = M<2.0; M(low) = 2.0; 5.0990 4.1231 3.1623 2.2361 2.0000 2.0000 2.0000 2.2361 3.1623 4.1231 5.0990 5.0000 4.0000 3.0000 2.0000 2.0000 2.0000 2.0000 2.0000 3.0000 4.0000 5.0000 5.0990 4.1231 3.1623 2.2361 2.0000 2.0000 2.0000 2.2361 3.1623 4.1231 5.0990 5.3852 4.4721 3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056 4.4721 5.3852 5.8310 5.0000 4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426 5.0000 5.8310 6.4031 5.6569 5.0000 4.4721 4.1231 4.0000 4.1231 4.4721 5.0000 5.6569 6.4031 7.0711 6.4031 5.8310 5.3852 5.0990 5.0000 5.0990 5.3852 5.8310 6.4031 7.0711 ] MATLAB Programming v= -5:5; col = [v;v;v;v;v;v;v;v;v;v;v]; row = col'; M = sqrt(row.^2 + col.^2); low = M<2.0; M(low) = 2.0; M= [ 7.0711 6.4031 5.8310 5.3852 5.0990 5.0000 5.0990 5.3852 5.8310 6.4031 7.0711 6.4031 5.6569 5.0000 4.4721 4.1231 4.0000 4.1231 4.4721 5.0000 5.6569 6.4031 5.8310 5.0000 4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426 5.0000 5.8310 5.3852 4.4721 3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056 4.4721 5.3852 DONE! Setting Up A Mesh This process of setting up a 2D grid is very common when dealing with 2D functions. We often want to create two matrices x and y where x will contain the x value of the grid point and y will contain the y value. -2 1 0 1 2 -2 2 2 2 2 -2 1 0 1 2 -1 1 1 1 1 x= -2 1 0 1 2 y= 0 0 0 0 0 -2 1 0 1 2 1 1 1 1 1 -2 1 0 1 2 2 2 2 2 2 col row Setting Up a Mesh There is a built in function to do this: [x,y]= meshgrid(xstart:xinc:xend, ystart:yinc:yend) Once the grid is set up then computing an arbitrary function as a function of the x and y value is easy z = (x.^2 + y.^2); 5 MATLAB Programming [col, row] = meshgrid(-5:5, -5:5); meshgrid(M = sqrt(row.^2 + col.^2); low = M<2.0; M(low) = 2.0; This is equivalent to the last program. Which vectorizes this program? for a = 1:size(M,1) for b = 1:size(M,2) if( mod(a,2)==0 and mod(b,3)==0) M(a,b) = M(a-1,b-1); M(a-1,bend end end Next Lecture 2D/3D Data Representation 6 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online