
494 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> [1:10].^2
ans
=
1 4 9 16 25 36 49 64 81 100
What happens here is that
[1:10]
generates a vector consisting of the numbers 1 to 10;
and the dot operator
.^2
squares each element in turn.
Vectorization can also be used with logical operators; we can obtain all positive elements
of the matrix
a
above with:
MATLAB/Octave
>> a>0
ans =
1 0 0 1
1 1 0 1
1 0 0 0
0 1 0 1
The result consists of 1’s only in the places where the elements are positive.
MATLAB and Octave are designed to perform vectorized commands very quickly, and
whenever possible such a command should be used instead