Introduction to MATLAB and Octave 489
The
reshape function produces a matrix with elements taken column by column from the
given matrix. Thus:
MATLAB/Octave
>> c = [1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20]
c =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
>> reshape(c,2,10)
ans =
1 11 2 12 3 13 4 14 5 15
6 16 7 17 8 18 9 19 10 20
>> reshape(c,5,4)
ans =
1 7 13 19
6 12 18 5
11 17 4 10
16 3 9 15
2 8 14 20
Reshape produces an error if the product of the two values is not equal to the number of
elements of the matrix. Note that we could have produced the original matrix above with
MATLAB/Octave
>> c = reshape([1:20],5,4])
All these commands work equally well on vectors. In fact, MATLAB makes no distinc-
tion between matrices and vectors; a vector merely being a matrix with the number of rows
or columns equal to 1.
The Dot Operators
A very distinctive class of operators in MATLAB are those that use dots; these operate
in an element-wise fashion. For example, the command
a
*
b
performs the usual matrix multiplication of a and b. But the corresponding dot operator:
a.
*
b
produces the matrix whose elements are the products of the corresponding elements of a
and b. That is, if
c=a.
*
b
490 A Computational Introduction to Digital Image Processing, Second Edition
then c(i, j) = a(i, j) × b(i, j):
MATLAB/Octave
>> a.
*
b
ans =
8 -8 28 -28
5 30 -9 -4
6 64 25 18
0 -18 0 -1
We have dot division and dot powers. The command a.^2 produces a matrix each element
of which is a square of the corresponding elements of
a:
MATLAB/Octave
>> a.^2
ans =
16 4 16 49
1 25 9 4
36 64 25 36
49 9 0 1
Similarly we can produce a matrix of reciprocals by writing 1./a:
MATLAB/Octave
>> 1./a
ans =
0.2500 -0.5000 -0.2500 0.1429
1.0000 0.2000 -0.3333 0.5000
0.1667 -0.1250 -0.2000 -0.1667
-0.1429 0.3333 Inf 1.0000
The value Inf is MATLAB’s version of infinity; it is returned for the op eration 1/0.
Operators on Matrices
Many functions in MATLAB, when applied to a matrix, work by applying the function
to each element in turn. Such functions are the trigonometric and exponential functions,
and logarithms. The use of functions in this way means that in MATLAB many iterations
and repetitions can be done with vectorization rather than by using loops. We will explore
this next.
Constructing Matrices
We have seen that we can construct matrices by listing all their elements. However, this
can be tedious if the matrix is large, or if it can b e generated by a function of its indices.
Two special matrices are the matrix consisting of all zeros, and the matrix consisting of
all ones. These are generated by the
zero and ones functions, respectively. Each function
can be used in several different ways:
Introduction to MATLAB and Octave 491
zeros(n) if n is a number, will produce a zeros matrix of size
n ×n
zeros(m,n) if m and n are numbers, will produce a zeros matrix
of size m × n
zeros(m,n,p,...) where m, n, p and so on are numbers, will produce
an m × n ×p × ··· multidimensional array of zeros
zeros(a) where a is a matrix, will produce a matrix of zeros
of the same size as
a.
Matrices of random numbers can be produced using the
rand
and
randn
functions. They
differ in that the numbers produced by
rand are taken from a uniform distribution on the
interval [0, 1], and those produced by
randn
are taken from a normal distribution with mean
zero and standard deviation one. For creating matrices, the syntax of each is the same as
the first three options of
zeros above. The rand and randn functions on their own produce
single numbers taken from the appropriate distribution.
We can construct random integer matrices by multiplying the results of
rand or randn
by an integer and then using the floor function to take the integer part of the result:
MATLAB/Octave
>> floor(10
*
rand(3))
ans =
8 4 6
8 8 8
5 8 6
>> floor(100
*
randn(3,5))
ans =
-134 -70 -160 -40 71
71 85 -145 68 129
162 125 57 81 66
The floor function will be automatically applied to every element in the matrix.
Suppose we wish to create a matrix every element of which is a function of one of its
indices. For example, the 10 ×10 matrix A for which A
ij
= i + j 1. In most programming
languages, such a task would be performed using nested loops. We can use nested loops in
MATLAB, but it is easier here to use dot operators. We can first construct two matrices: one
containing all the row indices, and one containing all the column indices:
492 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> rows=(1:10)’
*
ones(1,10)
rows =
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
>> cols=ones(10,1)
*
(1:10)
cols =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Now we can construct our matrix using rows and cols:
MATLAB/Octave
>> A=rows+cols-1
A =
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
The construction of rows and cols can be done automatically with the meshgrid function:
[cols,rows]=meshgrid(1:10,1:10)
will produce the two index matrices above.
The size of our matrix
a can be obtained by using the size function:
Introduction to MATLAB and Octave 493
MATLAB/Octave
>> size(a)
ans
=
4 4
which returns the number of rows and columns of a.
Vectorization
Vectorization refers to an operation carried out over an entire matrix or vector. We
have seen examples of this already, in our construction of the 10 ×10 matrix A above, and
in our use of the dot operators. In most programming languages, applying an operation
to elements of a list or array will require the use of a loop, or a sequence of nested loops.
Vectorization in MATLAB allows us to dispense with loops in almost all instances, and is
a very efficient replacement for them.
For example, suppose we wish to calculate the sine values of all the integer radians one
to one million. We can do this with a
for loop:
MATLAB/Octave
>> for i = 1:10^6, sin(i); end
and we can measure the time of the operation with MATLAB’s tic, toc timer: tic starts
a stopwatch timer, and
toc
stops it and prints out the elapsed time in seconds. Thus, on
my computer:
MATLAB/Octave
>> tic, for i = 1:10^6, sin(i); end, toc
elapsed
_
time =
27.4969
We can perf orm the same calculation with:
MATLAB/Octave
>> i = 1:10^6; sin(i);
and print out the elapsed time with:
MATLAB/Octave
>> tic, i=1:10^6; sin(i); toc
elapsed
_
time =
1.3522
Note that the second command applies the sine function to all the elements of the vector
1:10^6, whereas with the for loop, sine is only applied to each element of the loop in turn.
As another example, we can easily generate the first 10 square nunmbers with:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.63.165