4.6 Arrays, Vectors, and Matrices
The name MATLAB is derived from MATrix and LABoratory. It is a matrix-
oriented language, and Octave has the same properties. Any variable initially is
regarded as a matrix, and a matrix with one column is a column vector, a matrix
with one row is a row vector, and a matrix with one element is a scalar. This may
sound complicated, but it really is simple once you understand the concept.
Matrices are written in square brackets.
Arrays of concentrations, such as the substrate concentrations of an experiment,
may be regarded as vectors. If one uses seven concentrations like 1, 2, 4, 10, 20, 40,
and 100 mM for an experiment, these concentrations would be entered as a state-
ment such as C0 ¼ [1, 2, 4, 10, 20, 40, 100] in Octave. Square brackets
define a matrix (in our case, a matrix with one row, which is a row vector). When
the elements are separated with a comma, it is a row vector, and when the elements
are separated with a semicolon, it is a column vector. The third concentration of the
array C0 is denoted as C0(3)in Octave. If you type this in the terminal window,
you will get the answer ans ¼ 4.
Let us consider two vectors, a row vector a ¼ [1,2,3] and a column vector
b ¼ [4;5;6] Octave is well suited for vector algebra, but the sample programs
listed in this book will use vectors mostly as arrays of concentrations. For these,
element-by-element multiplication is a useful operation, written as .* (dot
followed by an asterisk). The operation requires that the vectors contain the same
numbers of rows and columns. If you typed a.*b with the examples above, you
would get the error message “nonconformant arguments”, since a is a row vector,
whereas b is a column vector. The transpose operator is denoted as the character’
(the single quote or prime sym bol). It transposes a row vector into a column vector
and vice versa. Therefore, the product a.*b’ will give the row vector [a1*b1,
a2*b2, a3*b3] for the above example. Try it out.
Note that the usual matrix multiplication is denoted by the operator “*” (aster-
isk). In the above example, a*b is the matrix multiplication of the row vector
a times the column vector b. The resulting scalar product is the number
a1*b1+a2*b2+a3*b3, which in our example is 32.
A matrix may be written as a column vector of row vectors or a row vector of
column vectors . M ¼ [a; a; a], for example, would be a 3X3 matrix where all
three rows contain the same elements a1, a2, and a3. The elements of a matrix can
be addressed directly as M(Nrow,Ncolumn) with Nrow and Ncolumn denoting
the index number for the row and the column, respectively. Vectors can be regarded
as matrices with one row or one column, respectively. For the row vector a, the
element a2 may be addressed as a2 ¼ a(2) ¼ a(1,2), and for the column
vector b, the third element might be addressed as b3 ¼ b(3) ¼ b(3,1).
At this stage, it is best to try out your own examples directly in the octave
window. If you have run the program start.m before, you may want to type y’
and see the result. Or type M ¼ [x; 2*x; 3*x] Or type 5*3 and get ans ¼ 15.
ans is the variable name (short for “answer”) for the result of the last octave
38 4 Getting Started with Octave