![](http://images-20200215.ebookreading.net/23/1/1/9781482247336/9781482247336__a-computational-introduction__9781482247336__bg1fd.png)
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: