Vectorized (or Array) Operations

Vector operations in Matlab allow you to apply a "single"
command to an entire array. In fact what is happening is that
"single" command is applied over and over again to every
element of the array. Vectorized operations are
equivalent to for loops and all vectorized operations
can be replaced with for loops.

Vector (or Array) Operations

A "Vector" operation in Matlab is the ability to write
condensed code to apply an action to every element of an array
with a single line of code.

We all know the basic operators in Math: +, -, *, /, etc.

5 + 5 produces 10;

Vectorized operators look like these basic math
operators and generally do "almost" the same thing. But when
applied to an Array (or matrix), these operations are
performed over EVERY element of the array (very similar to our
notion of a "loop").

Here are some examples:

Adding a single value to every element in an array:

[5 6 7] + 3; % == [8 9 10]

Adding two arrays together (element by element):

[5 6 7] + [1 2 3]; % == [6 8 10]

Do not confuse vector operations with geometric vectors.
Geometric vectors represent an X and Y direction in space.
Vectorized operations apply a single command to every element
of an array. Geometric vectors are called Vectors, because
they are, in essence, "arrays of directions".

Vector Operators

The + sign works directly as a "vector operation" and as a
"scalar operation" (applying to a single value). Some operators have
two meanings and thus requires a special (new) operator
for vector math.

Division

Dividing every element by a single value is accomplished just using the / for division.

[5 6 7] / 10
ans =
.5000 .6000 .7000

Dividing every element in an array by a value in a
corresponding array is done using the ./ (dot slash)
notation.

[5 6 7] ./ [ 8 9 10 ]
ans =
0.6250 0.6667 0.7000

Matrix division which is roughly the same as A times the inverse of B

[5 6 7] / [ 8 9 10 ]
ans =
0.6694

Multiplication

Multiplying every element by a single value is accomplished just using the *.

[5 6 7] * 10
ans =
50 60 70

Multiplying every element in an array by a value in a
corresponding array is done using the .* (dot slash)
notation.

Vector Math vs. For Loops

As we have talked about before, FOR loops are used to execute a
section of code a known number of times. For example, say we have
a list of quiz scores and we decide to curve the final scores
by 5 points. We must add 5 points to every quiz. The following
use of the FOR loop will achieve this goal:

quiz_scores = [88, 98, 87, 76, 89, 93, 95];
for index = 1 : length(quiz_scores)
quiz_scores(index) = quiz_scores(index) + 5;
end
% the old value of the current index into the array is added
% to 5 and then stored in the same "bucket" overwriting the
% previous value

Because this type of operation is so common Matlab has a
special vector operation notation.

Using a For Loop

Example of a For loop to replace the sum function (which is
somewhat similar to a vector operation.)

Vector Operation

array_1 = [5 6 7];
result = sum( array_1 ); % result == 18;

Using a For Loop

result = 0; % initialize result.
% add the current value in the result variable to the current
% value in the array (at position i) and store this new value
% in the result varaible (overwriting the old value)
for i=1:length(array_1)
result = result + array_1(i);
end