Back to tutorial index

# Two dimensional arrays

## Introduction

clear all
format short e

Back to the top

## From one-dimensional arrays to higher dimensional arrays

In our discussion of one-dimensional arrays we have discussed the length of the arrays, which we can get using the length function, but we have said little about the shape of the arrays we have created. But you may have noticed that both the linspace and the colon operator : create arrays with one row, and multiple columns :

x = linspace(-1,1,11)
x =

Columns 1 through 6

-1.0000e+00  -8.0000e-01  -6.0000e-01  -4.0000e-01  -2.0000e-01            0

Columns 7 through 11

2.0000e-01   4.0000e-01   6.0000e-01   8.0000e-01   1.0000e+00


y = -1:0.2:1
y =

Columns 1 through 6

-1.0000e+00  -8.0000e-01  -6.0000e-01  -4.0000e-01  -2.0000e-01            0

Columns 7 through 11

2.0000e-01   4.0000e-01   6.0000e-01   8.0000e-01   1.0000e+00



You may have also noticed that when we generated an array of random numbers using the rand command, we always supplied two arguments to the function, as in

z = rand(1,5)
z =

4.6053e-01   9.0583e-01   2.6888e-01   4.5663e-01   8.8074e-01



Finally, using square brackets, we constructed arrays as

w = [3.4 5.6 7.8 9.1]
w =

3.4000e+00   5.6000e+00   7.8000e+00   9.1000e+00



In each of above examples, we constructed arrays whose length was the number of elements in the array. But in fact, each array also had a shape. Each of the arrays above is a $1 \times N$ array, where the 1 indicates the number of rows (one in each case), and $N$ is the length of the array.

The power of Matlab comes from its ability to easily manipulate two, three, and higher dimensional arrays. In the above example, neither linspace nor the colon operator, by themselves, can be used to construct higher dimensional arrays. But the rand can. For example,

A = rand(3,5)
A =

9.7401e-01   8.0644e-01   4.6511e-01   7.9029e-01   8.9577e-01
7.6409e-01   4.0639e-01   2.9454e-01   8.8223e-02   1.2597e-01
8.6185e-01   6.5680e-01   7.3780e-01   3.0990e-01   5.1078e-01



creates a $3 \times 5$ array, or an array with 3 rows and 5 columns.

We can even construct three dimensional arrays with the rand function:

B = rand(4,2,3)
B(:,:,1) =

2.2018e-01   2.4518e-01
5.7542e-01   6.0676e-01
6.4709e-01   9.8804e-01
5.8807e-01   6.3518e-01

B(:,:,2) =

5.7883e-01   9.0349e-01
1.2859e-01   8.9149e-01
1.1914e-01   7.9252e-01
1.2044e-01   8.6765e-01

B(:,:,3) =

8.0613e-02   7.4629e-01
6.4922e-01   9.7043e-01
2.9654e-01   3.9040e-01
6.5780e-01   2.5385e-01



Back to the top

## Shapes of arrays

The shape of two dimensional arrays can be characterized by how many rows and columns it has. For every array (including scalars, which, in Matlab, are just $1\times 1$ arrays) Matlab stores its dimensions and its number rows and columns. To find the shape of an array, we can use the size function. For example,

A = rand(4,3)
[r,c] = size(A);
fprintf('A has %d rows and %d columns\n',r,c);
A =

3.1007e-01   8.3246e-01   9.4747e-01
2.6971e-01   1.4253e-01   2.5119e-01
8.3690e-01   4.1856e-01   6.7433e-01
4.3130e-01   4.7907e-01   8.2338e-01

A has 4 rows and 3 columns


Two arrays have the same shape if they have the same number of rows and columns.

If we only ask for one return argument, we will get an array containing the length of both dimensions.

mn = size(A)
mn =

4     3



If we ask for the length of an array in which neither dimension is 1, we will get the length of the largest dimension. This comes in handy if you know your array has, for example, many more rows than columns (as if often the case with large data sets) and you simply want to know how many rows of data you have.

my_data = zeros(237,5);
length(my_data)
ans =

237



In Matlab, even string variables have a length. For example,

s = 'Hello, World';
length(s)
ans =

12



Back to the top

## Indexing two dimensional arrays

We have already seen how we can retrieve a single entry in a one-dimensional array. We simply use the parenthesis operator () with an argument indicating the entry we want.

v = (1:10).^2
v =

1     4     9    16    25    36    49    64    81   100



Retrieve the second entry in v

v(2)
ans =

4



Retrieving a single entry from a two dimensional array uses the () operator as well, but this time with two arguments, the desired row and column index.

A = magic(4)  % Create a 4x4 magic square
A =

16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1



Retrieve the entry in the first row, second column

A(1,2)
ans =

2


A(3,4)
ans =

12


A(2,3)
ans =

10



As with one dimensional arrays, we can also retrieve multiple entries in a two dimensional array at once. For example, to get the second row of the array A above, we use the : operator.

A(2,:)
ans =

5    11    10     8



The third column is obtained in analogous fashion:

A(:,3)
ans =

3
10
6
15



To obtain rows 1 and 3 at the same time, we can use integer arrays in exactly the same way that we used them in the vector case.

A([1 3],:)
ans =

16     2     3    13
9     7     6    12



To retrieve columns 2 and 3, we do the following

A(:,[2 3])
ans =

2     3
11    10
7     6
14    15



And finally, if we want the last column or row, we use the end keyword. The last column of A is

A(:,end)
ans =

13
8
12
1



and the last row is

A(end,:)
ans =

4    14    15     1



The : operator by itself, when used in arrays can be used to retrieve the entire array as one long vector. When used with row vectors, it has the additional effect of converting the array to a column vector.

A(:)
ans =

16
5
9
4
2
11
7
14
3
10
6
15
13
8
12
1


v(:)
ans =

1
4
9
16
25
36
49
64
81
100



Suppose we really wanted to see if A is a magic square. We need to see if the rows and columns each add up to the same number. There are many ways to do this in Matlab, but here, we will use a single for loop.

for i = 1:4,   % sum over rows
row_sum(i) = sum(A(i,:));
end

for j = 1:4,   % Sum over columns
col_sum(j) = sum(A(:,j));
end
% Row sum :
row_sum
row_sum =

34    34    34    34


% Column sum :
col_sum
col_sum =

34    34    34    34



The row sums and the column sums are equal, so A is a magic square.

Back to the top

## Assigning values to arrays

We use array indexing to not only select values from an array, but also to assign particular values to an array. For example, we can create an $4\times 3$ array of all 1s and then reassign the third row to contain values [1,2,3]

X = ones(4,3)
X =

1     1     1
1     1     1
1     1     1
1     1     1


X(3,:) = -rand(1,3)
X =

1.0000e+00   1.0000e+00   1.0000e+00
1.0000e+00   1.0000e+00   1.0000e+00
-3.6814e-01  -3.3628e-01  -5.7137e-02
1.0000e+00   1.0000e+00   1.0000e+00



The array on the right hand side should have the same shape as the array array indexed on the left hand side. In the above example, the use of the ': allows us to specify all the columns. The array on the left hand side, then should have the same number of columns as those in the array X.

Here is an example in which we re-assign the 'interior' entries of an array the value 7.

A = zeros(5,5)
A =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0


A([2:4],[2:4]) = 7
A =

0     0     0     0     0
0     7     7     7     0
0     7     7     7     0
0     7     7     7     0
0     0     0     0     0



We can assign the 'exterior' values of the array to a different value, but this will require two steps. First, we assign the first and last columns the value -3.

A(:,[1 end]) = -3
A =

-3     0     0     0    -3
-3     7     7     7    -3
-3     7     7     7    -3
-3     7     7     7    -3
-3     0     0     0    -3



And then we assign the top and bottom rows the same value.

A([1 end],:) = -3
A =

-3    -3    -3    -3    -3
-3     7     7     7    -3
-3     7     7     7    -3
-3     7     7     7    -3
-3    -3    -3    -3    -3



There are many instances in which we need to use arrays to store values, say from an iterative calculation that we are doing. In this case, we might use a for loop. The following simple example shows how we might use the series approximation to the function $\sin(x)$ to approximate the sine function. $$\sin(x) = \sum_{i=0}^{\infty} \frac{(-1)^{i} x^{2i+1}}{(2i+1)!}$$ We can show how the approximations get closer and closer to our desired value, and plot the convergence of the sum.

sinx = 0;
x = 1.2;
for i = 0:10,
n = 2*i+1;
sinx = sinx + (-1)^i*x^n/factorial(n);
approx(i+1,:) = [sinx abs(sinx-sin(x))];
fprintf('%2d %24.16f %12.4e\n',i,approx(i+1,1),approx(i+1,2));
end
 0       1.2000000000000000   2.6796e-01
1       0.9119999999999999   2.0039e-02
2       0.9327359999999999   6.9691e-04
3       0.9320250514285713   1.4035e-05
4       0.9320392703999999   1.8443e-07
5       0.9320390842607376   1.7065e-09
6       0.9320390859789461   1.1720e-11
7       0.9320390859671641   6.2172e-14
8       0.9320390859672265   2.2204e-16
9       0.9320390859672263   0.0000e+00
10       0.9320390859672263   0.0000e+00


Because we stored the values in an array, we can now plot our approximations.

close all;
plot(0:10,approx(:,1),'.-','markersize',30);
hold on;
plot([0 10],[sin(x) sin(x)],'k--');
xlim([0 10]);
title('Approximation to sin(x) using a Taylor series','fontsize',18);
xlabel('i','fontsize',16);
ylabel('Approximation i','fontsize',16);

Back to the top

## Array concatenation for two dimensional arrays.

We already had a simple introduction to array concatenation for one-dimensional arrays. Now let's extend this idea to two dimensional arrays. First, it will be convenient to use the square brackets to build column vectors as well as row vectors. We have already seen that

v = [1,2,3]
v =

1     2     3



builds a row vector. Using [] in a similar fashion, we can also create a column vector. This time, however, we must separate each entry by a semi-colon (;) to indicate that we want to "stack" the numbers on top of each other.

v = [1;2;3]
v =

1
2
3



In Matlab, this way building of arrays from existing components (numbers, in this case), is called "concatenation". Concatenation can either be done horizontally (as was done in the first example above) or vertically (as in the second example).

This idea of array concatenation extends naturally to situations where the components are not just numbers, but are themselves arrays. For example, suppose we wanted to build an array by stacking existing row vectors on top of each other. This can easily be done using vertical concatenation.

% Three row vectors
a = [1,2,3];
b = [4,5,6];
c = [7,8,9];

% Build A by vertical concatenation
A = [a; b; c]
A =

1     2     3
4     5     6
7     8     9



In a similar fashion, we can build an array from existing column vectors.

% Three column vectors
a = [1; 2; 3];
b = [4; 5; 6];
c = [7; 8; 9];

% Build A using horizontal concatenation
A = [a b c]
A =

1     4     7
2     5     8
3     6     9



Of course, array concatenation can fail if you try to stack arrays that are not "conformable", i.e. their sizes don't match. Try the following :

A = [1; [2 3]]


Error using lab_5 (line 315)
Error using vertcat
CAT arguments dimensions are not consistent.


In this case, we are trying to stack a single number on top of a 1x2 row vector. The number of columns in the components we are trying to stack do not match, and Matlab complains.

The following, however, works

A = [1 [2 3]]
A =

1     2     3



We can always horizontally concatenate two row vectors of different sizes.

Back to the top

Often, data will be stored in a local file on your harddrive. You can construct an array from this file by simply loading this file into Matlab using the load command. For example, suppose we have the file my_data.dat stored in the local working directory. We can load this file into an array called A using the command

A = load('my_data.dat');


The array A will then have the same number of rows and columns as the file. For example, suppose that the file 'A.dat' looks like this

% File A.dat
3.4  5.6
4.7  8.9
-5.1 6.7
-2.1 5.6


Then, we can load the file and assign the results to the variable A.

A = load('A.dat')
A =

3.4000e+00   5.6000e+00
4.7000e+00   8.9000e+00
-5.1000e+00   6.7000e+00
-2.1000e+00   5.6000e+00



The file name and the variable name do not have to match. Also, comments can be included in data files in the same way that they can be included in m-files.

Back to the top

## Lab exercises

Construct the arrays described below, using Matlab functions, array assignment, or array concatenation.
1. A $10\times1$ array of all 1s. $$\left[\begin{array}{cccccccccc}1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\end{array}\right]$$
2. A $2\times 9$ array in which the entries in the first row are all equal to $\pi$ and the entries in the second row are all equal to $e$. $$\left[\begin{array}{ccccccccc}\pi & \pi & \pi & \pi & \pi & \pi & \pi & \pi & \pi \\ e & e & e & e & e & e & e & e & e \end{array}\right]$$
3. A $5\times3$ array of random numbers.
4. A $5\times3$ array of random numbers between -1 and 1.
5. A $3\times4$ array of all 1s, with the exception of the second row, which has been replaced by a row of -4s.
6. A $7\times5$ array, in which the first 6 rows are random numbers between -1 and 1, and the last row is chosen so that the sum of all the rows is exactly a row of 0s.
7. A $10\times 10$ array with 1s in the first columm, 2s in the second column, 3s in the third column and so on.
8. A $7\times7$ array in which the outer 'layer' of values is 0, the next layer is all 1s, and the innermost entry is 3. $$\left[\begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 1 & 1 & 1 & 1 & 0\\ 0 & 1 & 2 & 2 & 2 & 1 & 0\\ 0 & 1 & 2 & 3 & 2 & 1 & 0\\ 0 & 1 & 2 & 2 & 2 & 1 & 0\\ 0 & 1 & 1 & 1 & 1 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{array}\right]$$
9. A diagonal matrix with the sequence $\left[1,2,3,4,5\right]$ on the diagonal. $$\left[\begin{array}{ccccccccc} 1 & 0 & 0 & 0 & 0 \\ 0 & 2 & 0 & 0 & 0 \\ 0 & 0 & 3 & 0 & 0 \\ 0 & 0 & 0 & 4 & 0 \\ 0 & 0 & 0 & 0 & 5 \end{array}\right]$$
10. Construct the inverse of the diagonal matrix above, without using the inv function.
11. A anti-diagonal matrix with the sequence $\left[1,2,3,4,5\right]$ on the anti diagonal. $$\left[\begin{array}{ccccccccc} 0 & 0 & 0 & 0 & 5 \\ 0 & 0 & 0 & 4 & 0 \\ 0 & 0 & 3 & 0 & 0 \\ 0 & 2 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 \end{array}\right]$$
12. Use the reshape command to create the following matrix : $$\left[\begin{array}{ccccccccc} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array}\right]$$
13. Use the flipud and fliplr command to create the following matrix : $$\left[\begin{array}{ccccccccc} 9 & 6 & 3 \\ 8 & 5 & 2 \\ 7 & 4 & 1 \end{array}\right]$$
14. Use the kron command to create the following matrix : $$\left[\begin{array}{ccccccccc} 1 & 1 & 2 & 2\\ 1 & 1 & 2 & 2\\ 3 & 3 & 4 & 4 \\ 3 & 3 & 4 & 4 \\ \end{array}\right]$$
15. Use the repmat and eye commands to create the following matrix : $$\left[\begin{array}{ccccccccc} 1 & 0 & 1 & 0\\ 0 & 1 & 0 & 1\\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ \end{array}\right]$$