Chapter Contents Previous Next
 Understanding the Language

## Assignment Statements

Assignment statements create matrices by evaluating expressions and assigning the results to a matrix. The expressions can be composed of operators (for example, matrix multiplication) or functions (for example, matrix inversion) operating on matrices. Because of the nature of linear algebraic expressions, the resulting matrices automatically acquire appropriate characteristics and values. Assignment statements have the general form

result = expression;
where result is the name of the new matrix and expression is an expression that is evaluated, the results of which are assigned to the new matrix.

### Functions as Expressions

Matrices can be created as a result of a function call. Scalar functions such as LOG or SQRT operate on each element of a matrix, while matrix functions such as INV or RANK operate on the entire matrix. For example, the statement
```   a=sqrt(b);
```
assigns the square root of each element of B to the corresponding element of A.

The statement

```   y=inv(x);
```
calls the INV function to compute the inverse matrix of X and assign the results to Y.

The statement

```   r=rank(x);
```
creates a matrix R with elements that are the ranks of the corresponding elements of X.

### Operators within Expressions

There are three types of operators that can be used in assignment statement expressions. Be sure that the matrices on which an operator acts are conformable to the operation. For example, matrix multiplication requires that the number of columns of the left-hand matrix be equal to the number of rows of the right-hand matrix.

The three types of operators are as follows:

prefix operators
are placed in front of an operand (-A).

infix operators
are placed between operands (A*B).

postfix operators
are placed after an operand (A').
All operators can work in a one-to-many or many-to-one manner; that is, they enable you to, for example, add a scalar to a matrix or divide a matrix by a scalar. The followingis an example of using operators in an assignment statement.
```   y=x#(x>0);
```
This assignment statement creates a matrix Y in which each negative element of the matrix X is replaced with zero. The statement actually has two expressions evaluated. The expression (X>0) is a many-to-one operation that compares each element of X to zero and creates a temporary matrix of results; an element of the temporary matrix is 1 when the corresponding element of X is positive, and 0 otherwise. The original matrix X is then multiplied elementwise by the temporary matrix, resulting in the matrix Y.

For a complete listing and explanation of operators, see Chapter 17, "Language Reference."

 Chapter Contents Previous Next Top