{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture+24

# Lecture+24 - Engineering 101 Engineering 101/29/07...

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 Engineering 101 Lecture 24 11/29/07 Subarrays and Vectorization Quote of the Day 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 ­ Mahatma Gandhi other. Project 7 Announcements Project 7 Announcements Printing multiple graphs/figures Put the ‘figure( );’ operator before the plot to make the plot permanent (multi_plots.m) Breaking up the time steps If the values are not easily divisible for plotting, use a rounded value as a “plotStep” 107 time steps with 4 plots plotStep = ceil(107/4) = ceil(26.75) = 27 plots at: 27, 54, 81, and 107 (always plot at last time step) Script Files Script Files A script file is created by typing your command line inputs into a file and saving it with a .m suffix (multi_plots.m) Variables in a script file have global scope Variables in a function file have local scope Input to a script file Input to a script file Define the variable in the command window prior to running the script file Define the variable inside the script file. It will remain “active” after the script file is finished. Ask for a value in the script using: variable_name = input(‘gimme some input!’); Using Subarrays 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 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) A : on its own selects all values x = [ 7 8 9 ; 1 2 3; ­1 ­2 ­3 ] y = x( :, 1:2 ) b=[1839] y= 7 1 -1 8 2 -2 Using Subarrays Using Subarrays You can also assign to subarrays arr = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ] 1 5 9 2 6 10 3 7 11 4 8 12 Using Subarrays 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 Using Subarrays 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 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 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 0 0 0 3 7 11 0 0 0 Which matrix will be created by: x = eye(5); x(3:end, 3:end) = 5; Exercise Exercise 1 1 0 0 0 0 0 1 0 0 0 00 00 55 51 50 0 0 5 0 1 2 1 0 5 5 5 0 1 5 5 5 55 55 55 55 55 5 5 5 5 5 3 1 0 0 0 0 0 1 0 0 0 00 00 55 55 55 0 0 5 5 5 Which matrix will be created by: x = eye(5); x(3:end, 3:end) = 5; Exercise Exercise 1 1 0 0 0 0 0 1 0 0 0 00 00 55 51 50 0 0 5 0 1 2 1 0 5 5 5 0 1 5 5 5 55 55 55 55 55 5 5 5 5 5 3 1 0 0 0 0 0 1 0 0 0 00 00 55 55 55 0 0 5 5 5 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 Exercise Exercise 1 2 3 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 Exercise Exercise 1 2 3 News Flash! Verizon Wireless opens its network to all devices and third­party applications …as long as it fits their standards Must use CDMA, not GSM, so no AT&T or T­ Mobile phones (i.e. no iPhone) Device doesn’t necessarily need to be a phone Would allow Google Android devices on their network without having to join the Open Handset Alliance Vectorizing Vectorizing It is possible to use for loops to perform mathematical 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 Vectorizing Expressions 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 ]; x= 1 5 8 7 10 y = x > 5; 9 742 x(y) = x(y)­2; y= 0 1 1 100 sine_plot.m Vectorizing Expressions 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); Which is an equivalent vectorized expression? for i = 1:100 a(i) = exp(­0.2 * i); end Example Example Which is an equivalent vectorized expression? for i = 1:100 a(i) = exp(­0.2 * i); end Example 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 g(i) = g(i+1); end Example 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 g(i) = g(i+1); end Example Example Example Example Which function subtracts 1 from every number that is divisible by s in matrix A? 1 2 3 4 Example Example Which function subtracts 1 from every number that is divisible by s in matrix A? 1 2 3 4 Which function swaps the elements in the even numbered Which function swaps the elements in the even numbered positions of a vector with those in the odd numbered positions? 1 2 3 4 Which function swaps the elements in the even numbered Which function swaps the elements in the even numbered positions of a vector with those in the odd numbered positions? 1 2 3 4 Vectorization is Critical to Effective 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 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) =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2 ­2 ­2 ­2 ­2 ­2 ­2 ­2 ­2 ­2 ­2 ­1 ­1 ­1 ­1 ­1 ­1 ­1 ­1 ­1 ­1 ­1 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 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); 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 low = [ 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 1 1 1 0 0 0 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 ] 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; 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 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; 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; DONE! Setting Up A Mesh 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 Setting Up a Mesh Setting Up a Mesh [x,y]= meshgrid(xstart:xinc:xend, ystart:yinc:yend) There is a built in function to do this: 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); MATLAB Programming MATLAB Programming [col, row] = meshgrid(­5:5, ­5:5); M = sqrt(row.^2 + col.^2); low = M<2.0; M(low) = 2.0; This is equivalent to the last program. mesh_surf.m Surf( ) Preview Surf( ) Preview [col, row] = meshgrid(­5:5, ­5:5); M = sqrt(row.^2 + col.^2); low = M<2.0; M(low) = 2.0; figure(1); surf(M); title('default axes'); figure(2); surf(row,col,M); title('correct axes'); Next Lecture Next Lecture 3D Data Representation / Mandelbrot ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online