vectorizedCode - Is using a built-in function the same as...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
What is vectorized code? Consider the two code fragments below. Both accomplish the same thing and use vectors, but one fragment is vectorized while the other is not. % Vectorized code % Non-vectorized code a= rand(1,4); a= rand(1,4); b= rand(1,4); b= rand(1,4); c= a + b; for k= 1:length(a) c(k)= a(k) + b(k); end Vectorized code refers to operations that are performed on multiple cells of a vector at the same time (in one statement). Note that the addition (arithmetic operation) in the left code fragment is performed on all (multiple) cells of the vectors a and b in one statement. The addition on the right code fragment, however, is performed on one cell of vector a and one cell of vector b at a time—it is through the use of the loop (and appropriate indexing) that all cells in the vectors are added.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Is using a built-in function the same as using vectorized code? No. For example, if you write the statement s=sum(v) , you are calling a function and that is not vectorized code. The function sum may or may not use vectorized code to do the summing, but the function call that you write is just that, a function call it does not perform an operation on multiple cells. Be sure to follow instructions on an exam! On some questions we want to see that you have mastered loops and indexing and therefore do not allow vectorized code. Similiarly, we may forbid the use of specific built-in functions on a question. A significant penalty will be applied if you do not follow instructions....
View Full Document

This note was uploaded on 02/15/2008 for the course CS 100 taught by Professor Fan/vanloan during the Fall '07 term at Cornell University (Engineering School).

Ask a homework question - tutors are online