• Non ci sono risultati.

Initialising Matrices Within MATLAB

Root Finding 4

Task 5.6 Given that the data

6.1.1 Initialising Matrices Within MATLAB

Before we proceed with a description of matrix operations it is important to first discuss how we set up and access matrices within MATLAB. The simplest method involves simply typing the elements:

>> A = [2 3 4; 5 4 3]

A =

2 3 4

1 Even if you are comfortable with the theory of matrices we still recommend you read this section as it provides the important details of the correct syntax for entering and manipulating matrices within MATLAB.

5 4 3

Here the elements in the first row are separated by a space and the end of the first row is denoted by a semicolon. The second row is then typed, again with a space between successive elements. The whole matrix is contained within square brackets. The variable A is now initialised to be a two-by-three matrix;

we can see this by using the command size(A).

>> size(A) ans =

2 3

Here the command size returns two values; the number of rows followed by the number of columns. In the main we have just let the answer to our calculations appear on the screen (in fact they are also put into a variable called ans). We could have used the code:

>> A = [2 3 4; 5 4 3];

>> b = size(A);

>> disp(b)

>> [rows,columns] = size(A);

>> disp([rows columns])

The variable b is actually a one-by-two matrix (that is, a row vector con-taining two elements). The elements of this vector are b(1) and b(2) and these are used to store the number of rows and columns of A, respectively. Thus we can determine the number of rows of matrix A, in this case 2, by simply typ-ing b(1) at the MATLAB prompt. We could also call the function size(A) and assign the values to a vector whose elements are called rows and columns.

This is done in the fourth line of code. In this example we again encounter the MATLAB function disp, which literally displays its argument (which in this case is a row vector); we shall again return to the disp command a little later.

We now return to the question of initialising matrices (and vectors) using the method of just typing in the elements. We need to take some care with vectors, as MATLAB differentiates between column and row vectors (as should we). To enter a column vector we could use any of:

>> column_vector = [4 5

6];

>> column_vector = [4;5;6];

>> column_vector = [4 5 6]’;

The first command is self-explanatory. In the second we enter the first row, terminated by a semicolon, and then the second row, terminated by a semicolon, etc. The third version first creates the row vector [4 5 6] and then takes its transpose (interchanges rows and columns) to produce the column vector. We can use any of these methods but since our ultimate aim will be to write computer code in MATLAB, rather than entering commands at the prompt, we will generally not use the first method. In general we shall, throughout the text, employ the second form.

We now turn our attention to how we access elements (or parts of a matrix).

Let us start with the matrix

>> A = [11 12 13 14; 21 22 23 24; 31 32 33 34];

so that

A =

⎝ 11 12 13 14 21 22 23 24 31 32 33 34

⎠ .

There is no significance to these numbers, other than that they provide an indication of their location within the matrix; we have purely used them so we will be able to check that we have got the correct elements. The simplest way of obtaining an element is using parentheses, so that A(2,3) returns the value in the second row, third column (in this case 23). Note also that it is necessary to use round brackets. This is exactly as we would expect, and follows the convention that ai,j refers to the element in the ith row and the jth column.

It is also possible to refer to a whole row (or column) of a matrix. For example

>> A(2,:) ans =

21 22 23 24

returns the second row of the matrix A. Here the colon indicates all the elements along a particular row. Alternatively to refer to a particular column we could use

>> A(:,4) ans =

14 24

34

to return the fourth column of matrix A. Note that in both cases the answers are the same “shape” as they would appear in the matrix, that is the second row has been returned as a row vector and the fourth column has been returned as a column vector.

With the colon operator we can create row vectors which can then be used as arguments in other commands. For instance

>> r = 1:3;

>> A(r,1) ans =

11 21 31

This yields the same result as A(1:3,1) or A(:,1). We could also use r = 1:2 to get the first two elements of the first column, or r = 1:2:3 to get the first and last (noting that r = 1:2:3 gives the row vector r = [1 3]). The possibilities are extensive; for example if we want to obtain the top left hand two-by-two corner of A, we could employ the following:

>> r = 1:2;

>> B = A(r,r) B =

11 12

21 22

We now return to the problem of initialising matrices. Although it is not necessary in MATLAB, it is good programming practise to set up an empty matrix before accessing the elements. In many programming languages this is crucial and is referred to as dimensionalising variables and the syntax for how this is done varies considerably between languages. There are many special commands in MATLAB which aid in the initialisation of matrices and we start looking at these by using the colon (which refers to the entire row or column, depending on context) in some examples.

Example 6.1 Enter the matrix in MATLAB

In order to achieve this we use the commands

>> A = zeros(4);

The first command here initialises the matrix A to be a four-by-four matrix whose elements are all equal to zero. The matrix we want to enter has the first row and column having the same elements 1, 2, 3, 4. We therefore set up a vector, in this case r, to contain these values. We now want to assign the first column of the matrix A which we can do with the third command. Notice here that we are now assigning to the matrix A to have the first column equal to the transpose of the vector r; we have used the transpose because we want a column vector. The final command assigns the first row of A to be equal to the vector r and to print out the resulting matrix A which, as we see, is precisely the matrix we required. We could have entered the matrix in many different ways. One way would be to simply type the matrix in element by element:

>> A = [1 2 3 4;

2 0 0 0;

3 0 0 0;

4 0 0 0];

This is fine for small matrices but not practical for when we need to set up larger matrices. Another variant which can be used to set up the matrix is

>> A = [1 2 3 4; 2 zeros(1,3); 3 zeros(1,3); 4 zeros(1,3)];

Here the command zeros(1,3) sets up a row vector (one-by-three) full of zeros.

This example serves to emphasise there is no unique way to go about setting up a particular matrix; some ways are more elegant (and sometimes less readable) than others.