EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 3 Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers.

Slides:



Advertisements
Similar presentations
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Advertisements

Introduction to Matlab
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with MATLAB Lecture 20.
© 2005 Baylor University Slide 1 Fundamentals of Engineering Analysis EGR 1302 Unit 1, Lecture B Approximate Running Time - 24 minutes Distance Learning.
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Maths for Computer Graphics
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 12 System of Linear Equations.
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Matlab Basics IEF 217a: Lecture 2 Fall 2002 Sigmon and Davis, chapters 1-7.
Click Here to Begin the Game CHOICE 1CHOICE 2CHOICE 3 CHOICE CHOICE
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Table of Contents Matrices - Multiplication Assume that matrix A is of order m  n and matrix B is of order p  q. To determine whether or not A can be.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Computer Science & Engineering 2111 Data Validation and Macros 1 CSE 2111 Lecture-Data Validation and Macros.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Lecture 28: Mathematical Insight and Engineering.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers User-Defined Functions1 – Part 1 Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 2: Sub-Arrays Friday 05 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Matlab Programming for Engineers
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 1 Monday 29 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.
 6. Use matrices to represent and manipulate data, e.g., to represent payoffs or incidence relationships related in a network.  7. Multiply matrices.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 3: Array Operations Monday 08 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 5: Applications – Vector Math Friday 12 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers 2D Plotting – Part II Friday 19 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Matrix Operations.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Complex Numbers & 3D Plots – Part 2 Friday 07 Nov 2014 EGR 115 Introduction to Computing for Engineers.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with.
CSE 455 : Computer Vision MATLAB 101 Getting Started with MATLAB.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Matrix Multiplication The Introduction. Look at the matrix sizes.
Introduction to Programming on MATLAB Ecological Modeling Course Sep 11th, 2006.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Notes Over 4.2 Finding the Product of Two Matrices Find the product. If it is not defined, state the reason. To multiply matrices, the number of columns.
Converting Matrix Market matrices to Matlab format The Matrix Market is an interesting collection of matrices from a variety of applications.
Tutorial on Matlab Basics
Introduction to MATLAB
EGR 115 Introduction to Computing for Engineers
EGR 115 Introduction to Computing for Engineers
MATLAB DENC 2533 ECADD LAB 9.
Chapter 1 Introduction.
funCTIONs and Data Import/Export
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to MATLAB
Matrix Multiplication Sec. 4.2
Presentation transcript:

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