![]() |
References.These links will work on COGS machines but possibly not elsewhere.[ Working with Matrices | More About Matrices and Arrays ] |
Vectors.Sometimes it is much more convenient to group numbers that have common attributes and treat them like one. Take for example the co-ordinates of a single point in the 2D space (fig 1). Instead of having two separate numbers to describe the point, we use a vector (3,2), which describes its position. A vector is 1-dimensional structured collection of numbers that may have either of the following forms: Matlab supports vectors and you only have to type [3,2] or [3;2] respectively to enter vectors a and b. We say that the dimensions of vector a are 1x2 (one row, two columns) and the dimensions of vector b are 2x1 (two rows, one column). The arithmetic operations +, -, * , / can be applied to vectors: Addition between two vectors simply means adding each element of one vector to the corresponding element of the other:
[ 3 2 ] + [ 5 7 ] = [ 8 9 ] since 3+5=8 and 2+7=9. Of course vectors should be of the same dimensions (here 1x2). Addition between a vector and a number in Matlab will result in adding the number to each element of the vector: >> a=[1 2]; Subtraction is similar to addition: >> [3 2] - [5 7] In vector cases, however, we may have two kinds of multiplication: "point to point" multiplication and true vector multiplication. Point to point multiplication ( operator .* ) is as simple as addition and subtraction: >> [3 2] .* [5 7] since 3*5=15 and 2*7=14. Vector multiplication (operator *) is a bit more complicated. In this case the vectors should not be of the same dimensions but they need to have some sort of relation: If the dimensions of vector a are 1x2 then the dimensions of vector b should be 2x1 in order to multiply them. In other words the number of columns of the first vector should be the same as the number of rows of the second vector. The result will be a vector of dimensions 1x1 (single element). You can also multiply a vector with a simple scalar (using either .* or * the result will be the same). Such an operation will result in a vector where every element is multiplied with the number: >> a=[3 5]; Point to point division ( operator ./ ) can be defined between a number and a vector or a vector and a vector in a similar way to point to point multiplication: >> b=[2 10]; Division between vectors is not defined. A vector to the power of a number will have as a result a new vector whose elements will be the elements of the previous one at that power. Here the operator is .^, similar to the previous cases. In Matlab basic functions like cos and exp are designed taking into account that the input argument could be a vector. Therefore cos or exp with input argument a vector will have as a result a vector of the same dimensions, each element of which will be the cosine or exponential of the corresponding element of the original vector. >> x=[1:0.5:3]
Matrices.In many cases, we need to address problems more complicated than this network. There is a more general form than vectors that can be used in such cases, called a matrix. A matrix has the following form: And we can refer to an element separately by using the number of the row and column to which it belongs. For example 27 belongs to row 3 column 5, therefore we can refer to it: A3,5. To enter matrix A into Matlab: >> A=[ 3 6 8 9 12; 4 14 5 17 2; 1 7 15 11 27] Operations on matrices are similar to vector operations. For example addition: >> B=[ 1 2 3; 4 5 6] There is also "point to point" multiplication and division for matrices. The only operation that is slightly more complicated is true matrix multiplication. The same rule applies here as well: The number of columns of the first matrix should be the same as the number of rows of the second matrix. The result of such a multiplication is a matrix (R) with rows the number of rows of the first matrix and columns the number of columns of the second matrix. Element R1,2 of the matrix will be calculated by taking the 1st row of the first matrix and multiply it with the 2nd column of the second matrix. Note that a single row or a single column of a matrix is a vector. Therefore, to calculate each element of the matrix R, you have to make one vector multiplication. In general, to calculate element Rm,n you need to multiply the m row of the first matrix with the n column of the second matrix. An easy way to calculate all the elements is first to pre-decide the dimensions of the matrix to be calculated and then multiply the 1st row of the first matrix with the 1st column of the second and write it as element R1,1, then the 1st row of the first matrix with the 2nd column of the second and write the result as element R1,2 etc till you finish with all the columns of the second matrix. Then go to the second row of the first matrix and do the same till you finish with all rows of the first matrix. Matlab will do this for you, faster.
>> A=[ 1 2 3; 1 2 2]
In neural networks matrices are quite useful, as there are cases where we have to deal with complex structures. When we have to deal with a network with more than one layer, like the one in the next exercise, we may need a matrix to store the weight values instead of a simple vector. In this case, we use the following notation: We set up a matrix W and store the weights in such a way that for example w2,1 is the weight from node 1 to node 2. If there is no such weight, we put 0 instead.
|
[ Lab 1 | Lab 2 | Lab 3 | Lab 4 ] [ General Info | Lab Menu | COGS ] |
© May 2001 h.vassilakis |