Expressions

Similar to other programming languages, MATLAB also provides mathematical expression functionality. However, unlike most programming languages, these expressions are primarily designed to operate on matrices.

Numeric Representation

MATLAB uses traditional numeric representation methods. For example, for relatively long numbers, scientific notation is used; imaginary numbers use i or j as suffixes.

[Example 3-1] Some legal numeric representations:

3 -99 0.0001

9.6397238 1.60210e-20 6.02252e23

1i -3.14159j 3e5i

All numeric values are saved internally in MATLAB as the long format specified by the IEEE floating-point standard. The precision of floating-point data is 16 decimal places, with a range of approximately

Document Image

to

Document Image

.

Operators

1. Arithmetic Operators

Arithmetic operators can perform operations such as the four basic arithmetic operations, power, transpose, etc. The arithmetic operators used in MATLAB expressions are shown in Table 3-1.

Table 3-1: Arithmetic Operators Used in MATLAB Expressions

Operator Description Operator Description
+ Addition \ Left division
- Subtraction ^ Power
* Multiplication ' Complex conjugate transpose
/ Right division () Specify order of calculations

2. Comparison Operators

Comparison operators are used to compare the magnitude of two numbers, as shown in Table 3-2.

Table 3-2: Comparison Operators

Operator Description Operator Description
< Less than >= Greater than or equal to
<= Less than or equal to == Equal to
> Greater than ~= Not equal to

3. Logical Operators

Logical operators are used to judge certain logical relationships between objects or among objects.

[Example 3-2]

Assume the following 3 arrays and matrices:

code.matlab
A = [0 1 1 0 1];
B = [1 1 0 0 1];
C = [0 1 2;
     0 -3 8;
     0 5 0];

Table 3-3 lists the logical operation results for various operators and corresponding functions.

Table 3-3: Logical Operators

Operator Function Description Example
& and Returns 1 when values at the same position in both arrays are non-zero, otherwise returns 0. A & B = [0 1 0 0 1] or and(A,B)
` ` or Returns 1 when at least one value at the same position in the two arrays is non-zero, otherwise returns 0.
~ not Inverts elements in the array, i.e., non-zero values become 0, 0 becomes 1. ~A = [1 0 0 1 0] or not(A)
xor xor Returns 1 when only one element at the same position in the two arrays is non-zero, otherwise returns 0. xor(A,B) = [1 0 1 0 0]
any(C) If any element in the vector is non-zero, returns 1; otherwise returns 0. any(C) ans = 0 1 1
all(C) If all elements in the vector are non-zero, returns 1; otherwise returns 0. all(C) ans = 0 1 0
&& Returns true (1) if both expressions on either side are true; otherwise returns 0. a=8; a>5 && a<10 ans=1

4. Operator Precedence

According to priority levels, the order of various operators is as follows:

Parentheses ();

Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^);

Unary plus (+), unary minus (-), logical NOT (~);

Element-wise multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\);

Addition (+), subtraction (-);

Colon operator (:);

Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=);

Element-wise AND (&);

Element-wise OR (|);

Short-circuit AND (&&);

Short-circuit OR (||).

Processing String Expressions

Processing string expressions requires the use of the eval function and the feval function.

The eval function can process strings containing MATLAB expressions, statements, or function calls. The simplest syntax format for using this function is:

matlab

eval('string')

[Example 3-3] Use the eval function to generate an n-th order Hilbert matrix.

code.matlab
t = '1/(m + n - 1)';
k = 3;
for m = 1:k
    for n = 1:k
        a(m,n) = eval(t);
    end
end

The difference between the feval function and the eval function is that the feval function executes a function rather than a MATLAB expression. The function to be executed is specified in the first parameter via a function handle or a string containing the function name.