MATLAB - Operators
MATLAB - Operators
Section titled “MATLAB - Operators”An operator is a symbol that instructs the compiler to perform a specific mathematical or logical manipulation. MATLAB’s operators are designed to work seamlessly on both scalars (single numbers) and arrays/matrices, making it a powerful tool for numerical computation.
MATLAB’s elementary operations can be categorized as follows:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operations
- Set Operations
Arithmetic Operators
Section titled “Arithmetic Operators”A crucial concept in MATLAB is the distinction between matrix arithmetic and array arithmetic. Array operations, denoted by a preceding dot (.), are performed element-by-element.
| Operator | Description |
|---|---|
| + | Addition. A + B. Works element-wise. |
| - | Subtraction. A - B. Works element-wise. |
| * | Matrix multiplication. Follows the rules of linear algebra. The number of columns in A must equal the number of rows in B. |
| .* | Array (element-wise) multiplication. A .* B. A and B must have the same dimensions, or one must be a scalar. |
| / | Matrix right division. A / B is roughly equivalent to A * inv(B). |
| ./ | Array (element-wise) right division. A ./ B. Divides each element of A by the corresponding element of B. |
| \ | Matrix left division. A \ B is roughly equivalent to inv(A) * B. This is the recommended way to solve the linear system Ax = B. |
| .\ | Array (element-wise) left division. A .\ B. Divides each element of B by the corresponding element of A. |
| ^ | Matrix power. A^p computes A to the power of p. |
| .^ | Array (element-wise) power. A .^ B. Raises each element of A to the corresponding power in B. |
| ‘ | Matrix transpose. A' is the linear algebraic transpose. For complex matrices, it is the complex conjugate transpose. |
| .’ | Array transpose. A.' transposes A without complex conjugation. |
Example: Matrix vs. Array Multiplication
Section titled “Example: Matrix vs. Array Multiplication”A = [1, 2; 3, 4];B = [5, 6; 7, 8];
% Matrix multiplication (1*5 + 2*7 = 19)C_matrix = A * B
% Array (element-wise) multiplication (1*5=5, 2*6=12, ...)C_array = A .* BOutput:
C_matrix = 19 22 43 50
C_array = 5 12 21 32Relational Operators
Section titled “Relational Operators”Relational operators compare arrays element by element and return a logical array of the same size, with 1 (true) or 0 (false) for each element.
| Operator | Description |
|---|---|
| < | Less than |
| <= | Less than or equal to |
| > | Greater than |
| >= | Greater than or equal to |
| == | Equal to |
| ~= | Not equal to |
Example: Relational Operations
Section titled “Example: Relational Operations”A = [1, 5, 6];B = [2, 5, 3];
% Find elements where A is greater than BL1 = A > B
% Find elements where A is equal to BL2 = A == BOutput:
L1 = 1x3 logical array
0 0 1
L2 = 1x3 logical array
0 1 0Logical Operators
Section titled “Logical Operators”MATLAB has two types of logical operators:
- Element-wise (
&,|,~): Operate on each element of logical arrays. - Short-circuit (
&&,||): Operate on scalar logical expressions and are used in control flow statements likeifandwhile. They evaluate the second operand only if the result is not fully determined by the first.
Example: Logical Operations
Section titled “Example: Logical Operations”% Element-wise ANDA = [1, 0, 1]; % Using 1 and 0 for true and falseB = [0, 0, 1];C = A & B % Returns a logical array
% Short-circuit AND in an if statementx = 5;y = 10;if (x > 0) && (y > 5) disp('Both conditions are true.');endOutput:
C = 1x3 logical array
0 0 1
Both conditions are true.Bitwise Operations
Section titled “Bitwise Operations”Bitwise operators perform bit-by-bit operations on integer types.
| Function | Purpose |
|---|---|
| bitand(a, b) | Bit-wise AND of integers a and b |
| bitor(a, b) | Bit-wise OR of integers a and b |
| bitxor(a, b) | Bit-wise XOR of integers a and b |
| bitcmp(a) | Bit-wise complement of a |
| bitshift(a, k) | Shifts bits. Positive k is left shift, negative is right shift. |
Example: Bitwise AND
Section titled “Example: Bitwise AND”% A = 60 (binary 00111100)% B = 13 (binary 00001101)a = uint8(60);b = uint8(13);
% Result should be 12 (binary 00001100)result = bitand(a, b)Output:
result = 12Set Operations
Section titled “Set Operations”MATLAB provides functions to perform set operations on vectors.
| Function | Description |
|---|---|
| union(A,B) | Set union; returns elements that are in A or B, sorted. |
| intersect(A,B) | Set intersection; returns elements common to both A and B. |
| setdiff(A,B) | Set difference; returns elements in A that are not in B. |
| setxor(A,B) | Set exclusive OR; returns elements not in the intersection of A and B. |
| unique(A) | Returns the unique elements of A. |
| ismember(A,B) | Returns a logical array indicating which elements of A are also in B. |
Example: Set Operations
Section titled “Example: Set Operations”A = [1, 2, 3, 5];B = [2, 4, 5, 6];
% Union of A and BC_union = union(A, B)
% Intersection of A and BC_intersect = intersect(A, B)Output:
C_union = 1 2 3 4 5 6
C_intersect = 2 5