Vectorization

Matlab is written to deal with matrices and vectores. Because of that, it is much more efficient to solve problems solely by doing math on your vectors or portions of your vectors, rather than by ever needing to loop through particular elements in your vector and check their values.

You can actually check this out by timing something like taking the sine of a variable for 100 different values of the variable. The logical way to do it in matlab is to create a vector of those 100 different values, and take the sine of that vector. If you do it instead by a for loop from 1 to 100:

b = [some vector of 100 elements]
for n = 1:100,
	a(i) = sin(b(i));
end
it will take much longer. Sometimes you can't avoid using loops, but if you write your matlab scripts cleverly, you can make them much more efficient. Using logical conditions to select parts of your matrices (as we discussed in Lecture 1) is one way that is easy to take advantage of in vectorizing your problems.