Foundations of computer programming in MATLAB — Introduction to Arrays
What are arrays?
In MATLAB
, arrays are ordered lists of elements. Primarily, we will consider numeric arrays, in which those elements are numeric types (by default, they are of type double
, floating point numbers).
These objects have dimensions. This is simply a question of how the elements of the list are indexed. For instance, you could have a list of n
consecutive numbers indexed by a single index. In MATLAB
, this would be represented by a dimension “n
by 1
” or by a dimension “1
by n
” array. These are used to represent column- and row-vectors, respectively.
Alternatively, that same list of numbers could be indexed by two numbers. Say we divided up the original list into groups of length m
. We could refer to each group by an index, and each element of the group by another index. We could then talk about the element (i,j)
of the array, where i
is the group index and j
is the element index. What we have here is something similar to a matrix. If we say that the first index is the “row” index and the second the “column” index, then we have the MATLAB
way of representing matrices. The dimension of a matrix is the number of rows by the number of columns.
It actually keeps going: we can use even more indices, if we like. This can be very useful. The mathematical objects that these represent are called tensors, and one day a tensor may be just what we need. These higher-order arrays are called multidimensional arrays.
An example of a time we might use a multidimensional array is when we take a measurement that depends on three or more variable parameters. We can store each measurement with an index associated with each parameter. For instance, if we had three parameters with n
, m
, and l
values each, we could store our measurements in an n
by m
by l
array and access the values according to each parameter value.
Creating arrays
We can create an array in several different manners.
We can enter all its values, explicitly.
x1 = [ 1, 2, 3; 4, 5, 6 ];
This defines a 2
by 3
array. The commas separate row elements and the semicolons separate rows. It is also possible to use a space character to separate row elements. Extra spaces are, as usual, immaterial.
We can use a function to create a special type of array.
x2 = zeros(1,8); % [ 0 0 0 0 0 0 0 0 ]
x3 = ones(5,3,1,2); % 5 by 3 by 1 by 2 array with all elements 1
x4 = eye(4); % 4 by 4 identity matrix
MATLAB
vectors can be created in the same manner as other arrays, and there are a few extra functions that can be helpful, such as those that follow.
x5 = linspace(0,1e5,1e2); % 100 linearly spaced values from 0 to 1e5
x6 = logspace(1,5,100); % 100 logarithmically spaced values from 10^1 to 10^5
x7 = 0:0.1:10; % [ 0 0.1, 0.2, 0.3, ..., 9.9, 10]
We can find out what the dimensions of any array a
by calling size(a)
. For a one-dimensional array, size(a)
returns [1 n]
or [n 1]
.
Exercise
Build arrays in MATLAB
to represent the following vectors and matrices.
\begin{align}
\begin{bmatrix}
1 \\ 3 \\ -0.3 \\ 0
\end{bmatrix}
&&
\begin{bmatrix}
1 & 3 & -0.3 & 0
\end{bmatrix}
&&
\begin{bmatrix}
8 & 2 & 3 & 1 \\ 9 & 0 & -2 & 2
\end{bmatrix}
&&
\begin{bmatrix}
1 & 2 & \cdots & 1000
\end{bmatrix} \\[1em]
\begin{bmatrix}
1 \\ 2 \\ \vdots \\ 1000
\end{bmatrix}
&&
\begin{bmatrix}
0 & 0.2 & 0.4 & \cdots & 9.0
\end{bmatrix}
&&
\begin{bmatrix}
0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \
\end{bmatrix}
&&
\begin{bmatrix}
1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1
\end{bmatrix}
\end{align}
Accessing array elements
Array elements can be accessed via their indices, as follows.
x = [ 4 6 0 ];
y = x(1); % 4
z = x(2); % 6
w = x(3); % 0
a = eye(3); % identity matrix
b = a(1,1); % 1
c = a(1,2); % 0
d = a(2,1); % 0
e = a(2,2); % 1
f = a(end,end); % 1
This last line has a nicety. We can use the index end
to specify the last index value.
A “slice” of an array can be selected via the :
operator, as follows.
x = eye(3);
y = x(1,:); % [ 1 0 0 ]
y = x(2,:); % [ 0 1 0 ]
y = x(3,:); % [ 0 0 1 ]
A range from within an array can also be selected, as follows.
x = 0:2:10; % [ 0 2 4 6 8 10 ]
y = x([2 3 4]); % [ 2 4 6 ]
z = x(2:end); % [ 2 4 6 8 10 ]
Editing an existing array
It is easy to change the value of some array element.
x = 0:2:10; % [ 0 2 4 6 8 10 ]
x(4) = 0; % [ 0 2 4 0 8 10 ]
x(5:end) = 0; % [ 0 2 4 0 0 0 ]
This, we will find extremely useful.