Related Tags

matlab
communitycreator
arithmetic

What are arithmetic operators in MATLAB?

Zain Ali Babar

Operators are special keywords or characters that perform specific functions. MATLAB supports the use of operators on both scalar and non-scalar data.

Arithmetic operators
 Symbol﻿ Description + Addition or unary plus - Subtraction or unary minus * Matrix Multipliaction .* Element-wise Multiplication / Matrix Division Right ./ Element-wise Division Right \ Matrix Division Left .\ Element-wise Division Left ^ Matrix Power .^ Element-wise Power .' Matrix Transpose

For addition, the plus (+) operator is used:

// Addition

X = int32(5)
Y = X + 5 // Y will be 10

X = int32([10 20 30])
Y = X + 5 // Y will be [15 25 35]

X = int32([2 2])
Y = int32([3 3])
Z = X+Y // Z will be [5 5]


Subtraction

For subtraction, the minus (-) operator is used.

// Subtraction

//   subtract scalar from scalar
X = int32(5)
Y = X - 2 // Y will be 3

//   subtract scalar to array
X = int32([10 20 30])
Y = X - 5 // Y will be [5 15 25]

//   subtract array to array
X = int32([3 3])
Y = int32([1 1])
Z = X - Y // Z will be [2 2]


Multiplication

For multiplication, the * operator is used.

The .* operator is used for element-wise multiplication, while the * operator is used for normal multiplication.

// Multiplication

//   multiply scalar to scalar
X = int32(5)
Y = X * 2 // Y will be 10

//   multiply scalar to array
X = int32([10 20 30])
Y = X * 5 // Y will be [50 100 150]

//   multiply array to array element wise
X = int32([3 3])
Y = int32([2 2])
Z = X .* Y // Z will be [6 6]

//   multiply array to array
X = [1 0 1 0]
Y = [1; 2; 3; 4;]
Z = X * Y // Z will be 6


Right division

For the right division the / or ./ operator is used. This is further specified in the code below. The right division is conventional division. For example, 10/5 = 2.

The ./ operator is used for element-wise division, while the / operator is used for normal division:

// Right Matrix Division

//   divide scalar by scalar
X = double(5)
Y = X /   // Y will be 2.5
//   note that both ./ and / can be used here

//   divide scalar by array
X = double(5)
Y = [2 3 5]
Z = X ./ Y  // Z will be  [2.5000   1.6667   1.0000]

//   divide array by scalar
X = double(5)
Y = [2 3 5]
Z = Y ./ X  // Z will be  [0.4000   0.6000   1.0000]

//   divide array to array element wise
X = double([10 20 30])
Y = double([5 4 3])
Z = X ./ Y // Z will be [2 5 10]

//   divide array to array
X = double([10 20 30])
Y = double([5 4 3])
Z = X / Y // Z will be 4.4000


Left division

For the right division, the \ or .\ operator is used. This is further specified in the code below. The left division is of the form X/Y = inv(X) x B.

This is useful to calculate the solution of the equation, AX = B.

The ./ operator is used for element-wise division, while the / operator is used for normal division:

// Left Matrix Division

//   divide scalar by scalar
X = double(5)
Y = X \ 2  // Y will be 0.4000
//   note that both .\ and \ can be used here

//   divide scalar by array
X = double(5)
Y = [2 3 5]
Z = X .\ Y  // Z will be  [0.4000   0.6000   1.0000]

//   divide array by scalar
X = double(5)
Y = [2 3 5]
Z = Y .\ X  // Z will be  [2.5000   1.6667   1.0000]

//   divide array to array element wise
X = double([10 20 30])
Y = double([5 4 3])
Z = X .\ Y // Z will be [0.5000   0.2000   0.1000]

//   divide array to array
X = double([10 20 30])
Y = double([5 4 3])
Z = X \ Y // Z will be  0.035714   0.028571   0.021429
//                        0.071429   0.057143   0.042857
//                        0.107143   0.085714   0.064286


Power operator

The ^ and .^ operators calculate the power of the specified number/array.

The .^ operator is used for element-wise power, while the ^ operator is used for normal power functions:

// Power Operator

//   power of scalar
X = int32(5)
Y = X^2 // Y will be 25
// note that both .^ and ^ can be used here

//  scalar power of array
X = int32([2 3 10])
Y = X .^ 2  // Y will be now [4 9 100]
// note that both .^ and ^ can be used here

//  array power of array element-wise
X = int32([2 3 5])
Z = int32([2 3 1])
Y = X .^ Z  // Y will be [4 27 5]

//  array power of scalar
X = double([2 3; 1 2])
Y = double(2)
Z = Y ^ X // Z will be [ 7.2460   10.4650
//                         3.4883    7.2460 ]

//  Find inverse of matrix elements
X = double([1 2 3])
Y = X .^ -1 // Y will be [1.0000   0.5000   0.3333]


The transpose of a matrix

The .' operator calculates the transpose of a matrix. This interchanges the row and columns:

// Transpose of a Matrix
X = int32([10 20 30;
40 50 60;])
Z = X.'
// Z will be [10 40
//            20 50
//            30 60]


RELATED TAGS

matlab
communitycreator
arithmetic

CONTRIBUTOR

Zain Ali Babar
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time