Example of Vectorizing:
Likewise, for an operation on a matrix, a nested loop would be needed; for illustration, supposing a matrix variable mat:
[r c] = size(mat);
for row = 1:r
for col = 1:c
% do something with mat(row,col)
end
typically in MATLAB, this is not essential. The Numerical operations can be completed on whole vectors or matrices. For illustration, let's say that we want to multiply each and every element of a vector v by 3, and store the outcome back in v, here v is initialized as shown below:
>> v = [3 7 2 1];