Page 34 - Computational Colour Science Using MATLAB
P. 34
MATRIX OPERATIONS 21
places a copy of the original matrix M below the current contents of M and
hence produces a 4 2 matrix, whereas the command
>>M = [M M]
would produce a 2 4 matrix. Two matrices can be joined, side by side, provided
that they have the same number of rows. They can also be joined one on top of
the other, provided they have the same number of columns.
The colon operator is a special feature in MATLAB for constructing row
vectors of evenly spaced values. The statement
>>x = 1:6
x=
1 2 3 4 5 6
generates a row matrix x containing the integers from 1 to 6.
Individual elements of a matrix may be referenced by specifying their indices
within parentheses. Thus,
>>M = [1 1; 1 -1];
x = M(1,1)
x=
1
>>y = M(2,:)
y=
1-1
In the preceding statement the colon operator selects the whole row. Similarly,
y = M(:,2) would select the whole of the second column. It is also possible to
edit a single entry in a matrix by addressing it directly. Thus,
>>M = [1 1; 1 -1];
>>M(1,1) = 2;
M=
2 1
1 -1
Note that whole rows or columns can easily be selected and manipulated (copied,
printed, operated upon). For example, the statement M(1,:) = 2*M(1,:)
would double every entry in the first row of the matrix M.
MATLAB provides many functions for entering and manipulating special
matrices including linspace, ones, eye, inv, length, diag and size. As an example,
the command