MATLAB Operations on Matrices

Objective: To study arithmetic operation on matrices, relational operations on matrices, and logical operation on matrices.

Arithmetic Operators:

OperationsMATLAB FormComments
Array
Addition
a+bArray & matrix addition are identical
Array Subtractiona-bArray & matrix subtraction are identical
Array Multiplicationa.*bArray multiplication element by element multiplication of a & b
Matrix Multiplicationa*bFor matrix multiplication no of a column in matrix a=no of a column of matrix b
Array Right Divisiona./bElement by element division of a & b
Array Left Divisiona.\bElement by element division of a & b within the Numerator.
Matrix Right Divisiona/b a*inv(b) where inv represent inverse
Matrix Left Divisiona\binv(a)*b
Array Exponentiala.^bElement by element exponential of a & b i.e a(i , j) ^ b( i , j)

Examples:

Relational Operators:

Relational operators are used to representing conditions such as "space ? 0" and "result ? 25". They take two numerical (or string) operands. They yield a logical result (true or false).

The general form is: a1 op a2

a1 and a2 are arithmetic expressions, variables, or strings.

op is one of the following

OperatorsOperations
==Equal to
~=Not equal to
>Greater than
>=Greater than equal to
<Less than
<=Less than equal to

Examples

Logical Operators:

  • &&: Logical AND with shortcut evaluation.
    If the first expression is false, return false instead of calculating the second expression.
    It works only between scaler values.
    Faster because of partial evaluation.
  • &: Logical AND
    Evaluates both expressions.
    It works with both scaler and array values.
  • ||: Logical inclusive OR with shortcut evaluation.
    If the first expression is true, return true instead of calculating the second expression
    It works only between scaler values.
    Faster because of partial evaluation.
  • |: Logical OR
    Evaluates both expressions.
    It works with both scaler and array values.
  • XOR: Logical exclusive OR
    It returns true if and only if one operand is true and other is false.

Examples:

Logical functions:

  • ischar(a): It returns true if a is character array otherwise false.
  • isempty(a): It returns true if a is an empty array otherwise false.
  • isinf(a): It returns true if a is infinite otherwise false.
  • isnan(a): It returns true if a is not a number otherwise false.
  • isnumeric(a): It returns true if a is a numerical array otherwise false.
  • logical: It converts mathematical to logical.

Examples: