Command Reference : Matrix Language : Assigning Matrix Values
Assigning Matrix Values
Element assignment
Fill assignment
Matrix assignment
Scalar values on the right-hand side
Same object type on right-hand side
There are three ways to assign values to the elements of a matrix: you may assign values to specific matrix elements, you may fill the matrix using a list of values, or you may perform matrix assignment.
Element assignment
The most basic method of assigning matrix values is to assign a value for a specific row and column element of the matrix. Simply enter the matrix name, followed by the row and column indices, in parentheses, and then an assignment to a scalar value.
For example, suppose we declare the matrix A:
matrix(2,2) a
The first command creates and initializes the matrix A so that it contains all zeros. Then after entering the two commands:
a(1,1) = 1
a(2,1) = 4
we have
You can perform a large number of element assignments by placing them inside of programming loops:
vector(10) y
matrix (10,10) x
for !i = 1 to 10
y(!i) = !i
for !j = 1 to 10
x(!i,!j) = !i + !j
Note that the fill procedure provides an alternative to using loops for assignment (see, for example, the matrix object version of the procedure, Matrix::fill).
Fill assignment
The second assignment method is to use the fill object procedure to assign a list of numbers to each element of the matrix in the specified order. By default, the procedure fills the matrix column by column, but you may override this behavior to fill by rows.
You should enter the name of the matrix object, followed by a period, the fill keyword, and then a comma delimited list of values. For example, the commands:
vector(3) v
v1.fill 0.1, 0.2, 0.3
matrix(2,4) x
matrix.fill 1, 2, 3, 4, 5, 6, 7, 8
create the matrix objects:
If we replace the last line with
matrix.fill(b=r) 1,2,3,4,5,6,7,8
then X is given by:
In some situations, you may wish to repeat the assignment over a list of values. You may use the “l” option to fill the matrix by repeatedly looping through the listed numbers until the matrix elements are exhausted. Thus,
matrix(3,3) y
y.fill(l) 1, 0, -1
creates the matrix:
See Matrix::fill for a complete description of the fill procedure for a matrix. Equivalent procedures are available for the remaining matrix objects.
Matrix assignment
You can copy data from one matrix object into another using assignment statements. To perform an assignment, you should enter the name of the target matrix followed by the equal sign “=”, and then a matrix object expression. The expression on the right-hand side should either be a numerical constant, a matrix object, or an expression that returns a matrix object.
There are rules for how EViews performs the assignment which vary depending upon the types of objects involved in the assignment.
Scalar values on the right-hand side
If there is a scalar on the right-hand side of the assignment, every element of the matrix object is assigned the value of the scalar.
matrix(5,8) first
scalar second
vec(10) third
first = 5
second = c(2)
third = first(3,5)
Since declaration statements allow for initialization, you can combine the declaration and assignment statements. Examples:
matrix(5,8) first = 5
scalar second = c(2)
vec(10) third = first(3,5)
Same object type on right-hand side
If the source object on the right is a matrix or vector, and the target or destination object on the left is of the same type, the target will be resized to have the same dimension as the source, and every source element will be copied. For example:
matrix(10,2) zdata = 5
matrix ydata = zdata
matrix(10,10) xdata = ydata
declares that ZDATA is a matrix filled with 5’s. In the second line, YDATA is automatically resized to be a matrix and is filled with the contents of ZDATA.
The third line declares and initializes XDATA. Note that even though the declaration of XDATA calls for a matrix, XDATA is a matrix of 5’s. This behavior occurs because the declaration statement above is equivalent to issuing the two commands:
matrix(10,10) xdata
xdata = ydata
which will first declare the matrix XDATA, and then automatically resize it to when you fill it with the values for YDATA (see also “Copying Data From Matrix Objects”).
The matrix object on the right hand side of the declaration statement may also be the output from a matrix function or expression. For example,
sym eye4 = @identity(4)
declares the symmetric matrix EYE4 which is equal to the identity matrix, while
vector b = @inverse(xx)*xy
inverts the matrix XX, multiplies it by XY, and assigns the value to the new vector B.
The next section discusses assignment statements in the more general case, where you are converting between object types. In some cases, the conversion is automatic; in other cases, EViews provides you with additional tools to perform the conversion.