← MATLAB EnglishChapter 03 of 13

Operators

## Learning Objectives - Use arithmetic operators - Understand relational operators - Apply logical operators - Perform element-wise operations ## Arithmetic Operators ### Basic Operations ```matlab a = 10; b = 3; % Addition c = a + b % 13 % Subtraction c = a - b % 7 % Multiplication c = a * b % 30 % Division c = a / b % 3.3333 c = a \ b % 0.3 (left division) % Power c = a ^ b % 1000 (10^3) ``` ### Matrix vs Element-wise ```matlab A = [1, 2; 3, 4]; B = [2, 2; 2, 2]; % Matrix multiplication C = A * B % [6, 6; 14, 14] % Element-wise multiplication C = A .* B % [2, 4; 6, 8] ``` ### Element-wise Operators | Operator | Description | Example | |----------|-------------|---------| | `.+` | Element-wise addition | `A .+ B` | | `.-` | Element-wise subtraction | `A .- B` | | `.*` | Element-wise multiplication | `A .* B` | | `./` | Element-wise division | `A ./ B` | | `.^` | Element-wise power | `A .^ 2` | ### Increment/Decrement ```matlab x = 5; x = x + 1 % 6 x = x - 1 % 5 x += 1 % Not supported in MATLAB ``` ## Relational Operators ### Comparison Operators ```matlab a = 5; b = 10; % Equal c = (a == b) % false (0) % Not equal c = (a ~= b) % true (1) % Greater than c = (b > a) % true (1) % Less than c = (a < b) % true (1) % Greater than or equal c = (b >= 10) % true (1) % Less than or equal c = (a <= 5) % true (1) ``` ### Array Comparisons ```matlab A = [1, 2, 3]; B = [3, 2, 1]; % Compare element-by-element C = (A == B) % [0, 1, 0] % Compare with scalar D = (A > 2) % [0, 0, 1] ``` ### Floating Point Comparison ```matlab % Due to floating point precision a = 0.1 + 0.2; b = 0.3; a == b % false (0) % Use tolerance tol = 1e-10; abs(a - b) < tol % true (1) % Use isequaln for NaN handling isequaln(a, b) % false ``` ## Logical Operators ### Basic Logical Operators ```matlab a = true; b = false; % AND c = a & b % false (0) % OR c = a | b % true (1) % NOT c = ~a % false (0) % XOR c = xor(a, b) % true (1) ``` ### Short-circuit Operators ```matlab % && (short-circuit AND) % Stops evaluating if first condition is false % || (short-circuit OR) % Stops evaluating if first condition is true % Example x = 0; y = 10; if (x ~= 0) && (y/x > 5) disp('True'); else disp('False or error avoided'); end ``` ### Logical Functions ```matlab A = [1, 0, 3, 0, 5]; any(A) % true (non-zero exists) all(A) % false (not all non-zero) isnan([1, NaN, 3]) % [0, 1, 0] isinf([1, Inf, 3]) % [0, 1, 0] isfinite([1, Inf, NaN]) % [1, 0, 0] ``` ## Operator Precedence ### Precedence Order (highest to lowest) 1. Parentheses `()` 2. Transpose `.'` `.^` 3. Power `^` 4. Unary `+` `-` `~` 5. Multiplication `*` `/` `\` 6. Addition `+` `-` 7. Colon `:` 8. Comparison `<` `<=` `>` `>=` `==` `~=` 9. Element-wise `&` 10. Element-wise `|` 11. Short-circuit `&&` 12. Short-circuit `||` ```matlab % Example result = 2 + 3 * 4 % 14 (not 20) result = (2 + 3) * 4 % 20 ``` ## Set Operations ```matlab A = [1, 2, 3, 4]; B = [3, 4, 5, 6]; union(A, B) % [1, 2, 3, 4, 5, 6] intersect(A, B) % [3, 4] setdiff(A, B) % [1, 2] setxor(A, B) % [1, 2, 5, 6] ismember(3, A) % true (1) % Unique elements unique([1, 2, 2, 3]) % [1, 2, 3] ``` ## Bitwise Operations ```matlab a = 5; % Binary: 101 b = 3; % Binary: 011 bitand(a, b) % 1 (001) bitor(a, b) % 7 (111) bitxor(a, b) % 6 (110) bitcmp(a, 3) % 2 (010) - 3-bit complement % Shift bits bitshift(a, 1) % 10 (1010) - left shift bitshift(a, -1) % 2 (10) - right shift ``` ## Special Operators ### Colon Operator ```matlab 1:5 % [1, 2, 3, 4, 5] 0:0.5:2 % [0, 0.5, 1, 1.5, 2] 10:-2:4 % [10, 8, 6, 4] ``` ### String Concatenation ```matlab str1 = 'Hello'; str2 = 'World'; % Horizontal concatenation str = [str1, ' ', str2] % 'Hello World' str = strcat(str1, ' ', str2) % Vertical concatenation str = char(str1, str2) % 2x5 character array ``` ## Summary - Use `.*`, `./`, `.^` for element-wise operations - Relational operators return logical values (0 or 1) - Use `&&` and `||` for short-circuit evaluation - Operator precedence follows standard mathematical rules - Use parentheses to clarify intent - Floating point comparisons should use tolerance

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →