MATLAB Programming/Vectoring Mathematics

MATLAB is a vector programming language. The most efficient use of MATLAB will involve taking advantage of the built-in capabilities for manipulating data instead of using constructs such as loops.

Basic Math
Most arithmetic operators will work as expected on vectors:

Likewise, all of these operations can be done with matrices for the expected results.

Most MATLAB functions, such as  or , will return a vector or matrix of the same dimensions as its input. So to compute the sine of all the integers between 0 and 10, it suffices to run and the returned vector will contains ten values.

Per Element Operations
Operators such as the carrot (^) or multiplication between vectors may not work as expected because MATLAB sees vectors the same as any other matrix, and so performs matrix power and matrix multiplication. All operators can be prefixed by a  to make it explicit that an operation should be performed on each element of the matrix. For example, to compute the differences of the sine and cosine function squared for all integers between 1 and 4, one can use: as opposed to which results from MATLAB's attempt to square a 1x4 vector using matrix multiplication.

Using  or   allows one to divide each element of a matrix or vector by the elements of another matrix or vector. To do this, both vectors must be of the same size.

Converting Loops to Vector-based mathematics
Since MATLAB is a vector language, an artificial algorithm such as can be done far more efficiently in MATLAB by working with vectors instead of loops: Internally, MATLAB is of course looping through the vectors, but it is at a lower level then possible in the MATLAB programming language.