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

Info icon This 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.
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern