EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 3 Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers
Lecture Outline Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Nested Loops Profiling Slide 2 of 11
Loops and Vectorization Nested Loops Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers The variety of statements which can be in a for or while loop includes other loops. Loops inside of loops are called “Nested Loops” for i = 1:4 for j = 1:3 fprintf('i=%g, j=%g\n',i,j); end i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 i = 4, j = 1 i = 4, j = 2 i = 4, j = 3 Slide 3 of 11
Loops and Vectorization Nested Loops Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Another Example: Matrix Multiplication Consider the program required to multiply two matrices i th row of A j th col of B Slide 4 of 11
Calculates the i th row of C Loops and Vectorization Nested Loops Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Looks like Nested loops!! for i = 1 to # of Rows of A (=n) for j = 1 to # of Cols of B (=m) for k = 1 to # Col of A (=r) c ij = c ij + a ij x b ji end Nested Loops A = [ % 3 X 4 matrix ]; B = [1 2 % 4 X 2 matrix ]; C = zeros(3, 2); % 3 X 2 matrix for i = 1:3 for j = 1:2 for k = 1:4 C(i,j) = C(i,j) + A(i,k)*B(k,j); end nested_loops.m Slide 5 of 11
Loops and Vectorization The MATLAB Profiler Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers A Profiler identifies the “hot spots” in your program Hot spots are code segments which consumes many CPU cycles Open the *.m file of interest (e.g., profile_example.m)profile_example.m o Click on “Run and Time” Slide 6 of 11
Loops and Vectorization The MATLAB Profiler Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Profiler Report Summary: Select program name Slide 7 of 11
Loops and Vectorization Nested Loops Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Shows where time was spent Slide 8 of 11
Loops and Vectorization textread function Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers The “textread” function allows us to read data from a text file More flexible than the “load” command o Load requires that all of the data be of the same type (e.g., double) textread requires that data is organized by column o Each column can be a different data type o Reads data from “filename” using specified format into variables “A”, “B”, “C”, … [A,B,C,...] = textread(filename,format) Slide 9 of 11
[First_Name, Last_Name, date, var, availibility] = … textread('test_data.txt', '%s %s %d %f %s') Loops and Vectorization textread function Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Given the file “test_data.txt” containing:test_data.txt Read the data using textread into variables First_Name, Last_Name, date, var, availibility steve Davis yes Matt Peters maby Pete Frank no Mike Jones sometimes Slide 10 of 11
Next Lecture Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers Updating our Battleship Game Remove the requirement of sharing our “my_grid” Improve the logic Automate the firing process Curve Fitting & Interpolation Slide 11 of 11