Upload
dayna-lyons
View
212
Download
0
Embed Size (px)
DESCRIPTION
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 ',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
Citation preview
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 VectorizationNested 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); endend
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 VectorizationNested Loops
Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Another Example: Matrix Multiplication Consider the program required to multiply two matrices
C AB
11 1211 12 13 14
21 2221 22 23 24
31 3231 32 33 34
41 42
b ba a a a
b ba a a a
b ba a a a
b b
11 12
21 22
31 32
c cc cc c
( , ) ( ,:) (:, )C i j A i B j
ith row of A jth col of B
3 4
4 2
3 2
n r
r m
n m
A
B
C
Slide 4 of 11
Calculates the ith row of CCalculates the element
Loops and VectorizationNested 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)
cij = cij + aij x bji
endend
end
ij ik kik
c a b
Nes
ted
Loo
ps
( , ) ( ,:) (:, )C i j A i B j
A = [1 2 3 4 % 3 X 4 matrix 5 6 7 8 9 10 11 12]; B = [1 2 % 4 X 2 matrix 3 4 5 6 7 7];
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 endend
nested_loops.m
Slide 5 of 11
Loops and VectorizationThe 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)
o Click on “Run and Time”
Slide 6 of 11
Loops and VectorizationThe 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 VectorizationNested Loops
Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Shows where time was spent
Slide 8 of 11
Loops and Vectorizationtextread 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 columno 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 Vectorizationtextread function
Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers
• Given the file “test_data.txt” containing:
• Read the data using textread into variables First_Name, Last_Name, date, var, availibility
steve Davis 10 3.5 yesMatt Peters 22 1.1 mabyPete Frank 17 2.5 noMike Jones 12 0.9 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