# Basic Operations in MATLAB

March 11, 2021 2021-03-12 2:48## Basic Operations in MATLAB

# Basic Operations in MATLAB

## What is MATLAB?

**MATLAB **abbreviated form of ‘* Matrix Laboratory*‘ is proprietary software, widely used by students, engineers, scientists, etc. for various types of mathematical computations.

It has been developed by **MathWorks**.

The most important aspect of this software is **MATLAB **programming language which is a *matrix-based* language.

Like any other high-level programming language, MATLAB also demands some basic knowledge of concepts like variables, constants, expressions, statements, etc.

## History of MATLAB

Initially, MATLAB was invented by Cleve Barry Moler who was a mathematician and a computer programmer. He was an expert in numerical analysis.

He proposed the idea of MATLAB in his Ph.D. thesis in the 1960s. Later he became a professor at the University of New Mexico and started developing MATLAB for his students.

The early version of MATLAB was completed in the early 1970s. But it was disclosed for public use for the first time in 1979.

The early version of MATLAB was like a simple ‘matrix calculator’ with 71 pre-built-in functions. At that time it was distributed free of cost to Universities.

Finally, MATLAB was reprogrammed in C language, and it was first released as a commercial product in 1984 and MathWorks was founded to develop the software and the MATLAB programming language was released.

## Why you should learn MATLAB programming?

As we know it is widely used by students, engineers, and scientists for numerical computation, visualization, and application development.

Following are some of the major domains where MATLAB is used:

- Digital Image Processing
- Video Processing
- Signal Processing
- Communication System Designing
- Machine Learning & Deep Learning
- Computational Finance

### Although we can perform a variety of operations on MATLAB, to keep this tutorial beginner friendly we will discuss only the Basic mathematical operations.

## Basic mathematical operations on MATLAB

### 1. Simple Arithmetic Operations

```
% Addition (+) of Numbers
>> 3 + 5
ans =
8
>> 3 + 5 + 7 + 11
ans =
15
% Subtraction (-) of Numbers
>> 7 - 2
ans =
5
>> 7 - 2 - 1 - 12
and =
-8
% Multiplication (*) of Numbers
>> 3 * 5
ans =
15
>> 3 * 5 * 9 * 2
ans =
270
% Division (/) of Numbers
>> 21 / 7
ans =
3
>> 63 / 3 / 7 / 3
ans =
1
% Power (^) of Numbers
>> 5 ^ 3
ans =
125
>> 2 ^ 3 ^ 3 ^ 2
ans =
262144
```

### 2. Mathematical Constants

```
% Pi
>> pi
ans =
3.1416
% exponential 'e' = e
```^{1}
>> exp(1)
ans =
2.7183
% iota or unity complex number (i or j)
>> i
ans =
0.0000 + 1.0000i
>> j
ans =
0.0000 + 1.0000i
% Natural logarithm of 10 i.e log_{e}(10)
>> log(10)
ans =
2.3026

### 3. Vectors & Matrices

```
% Row vectors
>> a = [1 2 3 4 5]
a =
```` 1 2 3 4 5`

>> b = [1, 2, 7, 8, 11, 13]
b =
` 1 2 7 8 11 13`

% Column Vectors
>> p = [1; 3; 5; 7; 9; 11]
p `=`

`1`

`3`

5
`7`

`9`

`11`

>> q = [2, 5, 7, 9, 11]'
q =
`2`

`5`

`7`

`9`

`11`

% Matrices (N x M)
>> m = [1 2 3; 4 5 6; 7 8 9]
m =
`1 2 3`

`4 5 6`

`7 8 9`

>> d = [1, 1, 3, 5; 2, 3, 3, 6; 7, 8, 9, 9]
d =
`1 1 3 5`

`2 3 3 6`

`7 8 9 9`

### 4. Arithmetic Operations on Matrices

```
% Addition of matrices (A & B)
>> A + B
% Subtraction of matrices (A & B)
>> A - B
% Multiplication of matrices (A & B); No. of columns of A = No. of rows of B
>> A * B
% Multiplication & Division of a scalar (a) and a matrix (a)
>> a * A
>> A * a
>> A / a
% Element wise product & division of matrices (A & B); size of A = size of B
>> A .* B
>> A ./ B
% Transpose of a matrix (A)
>> A'
% Rank of a matrix (A)
>> rank(A)
```

### 5. Mathematical Commands

```
% sin of x where x is in radians
>> sin(pi / 2)
ans =
1
% cos of x where x is in radians
>> cos (pi)
ans =
-1
% Natural logarithm
>> log(2)
ans =
````0.6931`

% Common logarithm
>> log10(1000)
ans =
`3 `

% Exponential
>> exp(5)
ans =
148.4132

### 6. Basic Utility Commands

```
% clears the MATLAB command window
>> clc;
% clears all the stored data & variables from the MATLAB memory
>> clear;
% closes all the opened MATLAB figures' windows
>> close all;
% saves all variables from the current workspace in a MATLAB formatted (.m) file
>> save file_name.m
% loads all variables from a MATLAB formatted (.m) file in the current workspace
>> load file_name.m
% Takes inputs from a user
>> x = input('Give Input')
% This is a comment in MATLAB i.e any line starting with the '%' symbol is not processed for mathematical calculations
```

## Resources to learn MATLAB programming

Following are some of the good beginner-friendly online courses to learn MATLAB programming fundamentals: