Lecture 08

Lecture 08 - 14:440:127 Introduction to Computers for...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 08 Rutgers University, Fall 2009 Instructor- Blase E. Ur 1 Finishing Up Loops- Efficiency Concerns In Matlab, youll often hear that its bad to use loops when you could have instead used vector or matrix functions. This is because Matlabs built-in functions and arithmetic operations are optimized to use data stored as vectors or matrices in memory. Thus, whenever possible, use vectors, matrices, and their associated functions. Many of the examples Ive given you in this lecture and the previous lecture could be written more efficiently using built-in functions and by vectorizing (replacing loops with vector operations). Ive given you many examples using loops for two reasons. First of all, theyre often some of the simplest loops to write, and thus they serve a pedagogical purpose. As you start writing more complicated programs, youll often need to write loops, but these loops will be implementing much more complex algorithms. Second, most computer programming languages use loops much more extensively than Matlab, so being good at loops is a skill-set that you can port to other languages you learn. As with natural (human) languages, learning more programming languages becomes easier after your first one or two! 1.1 Timing Efficiency- Tic and Toc Matlab has two convenient commands that let you time how long an operation takes to compute. To start (and reset) a timer, use the command tic; . To stop the timer and display the amount of time elapsed, use toc; . If youve brainstormed a few different methods for solving a problem and want to have an idea of which method runs most quickly in Matlab, use tic and toc ! 1.2 Loops, Pre-allocated Memory Loops, Vector Operations As an example of the huge differences in efficiency between methods in Matlab, lets compare three different methods of performing the same operation in Matlab. Particularly, lets create a 10,000 element vector filled with the number 5 . Indeed, 5 is against the law. (Thats not actually true, thats just what happens when you type 5 is into Google and look at the list of auto-complete possibilities.) First, we can use the totally naive method and create this vector one element at a time: % Allocate memory one element at a time clear;clc tic for z = 1:10000 x(z) = 5; end toc This method takes 0.181933 seconds on my desktop. Now, lets pre-allocate memory for the vector were creating. By this, we mean that we want to create a ma- trix full of zeros thats the eventual size we want. That way, Matlab will set aside space in memory for the entire vector, rather than having to mess around with allocating memory each time. If youre interested in this subject, Id recommend you take more advanced classes in Computer Engineering or Computer Science such as Architecture, Operating Systems, and Compilers....
View Full Document

Page1 / 11

Lecture 08 - 14:440:127 Introduction to Computers for...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online