A matrix is a two-dimensional element array. When using the MATLAB platform, you may simply generate matrices by assigning array items separated by spaces or commas. To indicate the end of each row, use semicolons. In this article, you will read about it in detail to help you grasp it better.

Become a Data Scientist With Real-World Experience

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist With Real-World Experience

What Is a Matlab Matrix?

In Matlab, a matrix is a type of variable that is mostly used for mathematical calculation. This allows you to deal with matrix calculations effectively and quickly. As stated in the introduction, a matrix is a two-dimensional array in linear algebra that is related to analytics. 

The MATLAB platform includes several built-in functions that can be used to create matrixes and then assign values to them. Matlab software also supports various mathematical and trigonometric computations. Matlab's arithmetic operations on matrices include addition, subtraction, and multiplication. It also enables inverse operations for tan, cosec, sin, cos, sec, cot, and sin. Complex number computation and concatenation procedures for two matrix values are also supported.

Creating a Matlab Matrix

You may make a matrix by inputting components as commas or space-separated numbers in each row and using semicolons to indicate the end of each row. 

Example: To make an array with three elements in a row, divide the elements with a comma (,) or a space.

a= [ 1, 2, 3, 4 ]

Referencing the Elements of a Matrix

The following code is used to refer to an element in the nth column and mth row of a matrix mx:

mx(m, n);

For example, if you wish to refer to the top ay element, which is located in the 5th column and 2nd row of the matrix a, you may write it as below:

a(2,5);

Deleting a Row or a Column in a Matrix

If you wish to remove a whole row or column of a matrix, just surround it with an empty pair of square braces [ ]. Essentially, [ ] represents an empty array.

Example:

Let us remove the fourth row of a - 

a = [1 2 3 4 5 ;  2 3 4 5 6 ;  3 4 5 6 7;  4 5 6 7 8]

 a(4, :) = [ ]

The above command will be executed by MATLAB and the following result will be returned -

a = [1 2 3 4 5 ;  2 3 4 5 6 ;  3 4 5 6 7; ]

(In simple words, the fourth row will be totally deleted from the matrix)

Addition and Subtraction of Matrices

Adding and subtracting numbers is an important function that is frequently done in MATLAB. 

Note: Remember that both operand matrices should compulsorily have the same number of rows and columns.

You must first create an array and then apply values to it.

a = [1 2 3 ; 4 5 6; 7 8 9]; b = [7 5 6 ; 2 0 8; 5 7 1];

Now, just write the following to add the two matrices a and b you have top:

c = a + b

d = a - b

When you execute the file, it produces the following output:

c =[ 8 7 9; 6 5 14 ; 12 15 10]

d = [-6 -3 -3 ; 2 5 -2 ; 2 1 8]

Become a Data Scientist With Real-World Experience

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist With Real-World Experience

Division of Matrices

Following addition and subtraction, let's look at how you can easily divide two operand matrices in MATLAB. Left (\) and right (/) division operators can be used to divide two matrices. 

Note: Remember that both operand matrices must have the same number of rows and columns.

To begin, write the following code into a script file. You are essentially creating two arrays and then assigning values to them.

a = [1 2 3 ; 4 5 6; 7 8 9]; b = [7 5 6 ; 2 0 8; 5 7 1];

Now, if you wish to divide these two arrays, write the code below:

c = a / b

d = a \ b

When you execute the file, it produces the following output:

c =

   -0.52542   0.68644   0.66102

   -0.42373   0.94068   1.01695

   -0.32203   1.19492   1.37288

d =

   -3.27778  -1.05556  -4.86111

   -0.11111   0.11111  -0.27778

   3.05556   1.27778   4.30556

Scalar Operations of Matrices

Scalar operations are defined as operations that generate a new matrix with the same number of rows and columns as the original matrix, with each element of the original matrix added to, subtracted from, multiplied by, or divided by the number.

The scalar operation is basically used when you want to add, subtract, multiply, or divide a matrix by a number. Summarizing the code statements to perform addition, subtraction, multiplication or division are mentioned below:

c = a + b

d = a - b

e = a * b

f = a / b

Transpose of a Matrix

When you perform a transposition operation on a matrix, it shifts the rows and columns. It is denoted by a single quotation (').

Example: 

Make a script file using the following code:

a = [10 12 23 ; 14 8 6; 27 8 9]

When you execute the file, you will see the following result: b = a'

a =

      10    12    23

      14     8     6

      27     8     9

b =

      10    14    27

      12     8     8

      23     6     9

Concatenating Matrices

The primary goal of concatenating two matrices is to obtain a larger matrix which contains all the elements of those two matrices. The concatenation operator is represented by the square brackets '[ ]'.

MATLAB supports two kinds of concatenations.

Horizontal Concatenation

When you concatenate two matrices separated by commas, they are simply concatenated horizontally. It's known as horizontal concatenation.

Example c = [a, b]

Vertical Concatenation

Alternatively, if two matrices are concatenated by separating them with semicolons, they are added vertically. Vertical concatenation is the term for this process.

Example: c = [a; b]

Become a Data Scientist With Real-World Experience

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist With Real-World Experience

Matrix Multiplication

Multiplication is one of the most important concepts to understand in MATLAB. Consider the following two matrices: A and B. Consider A to be a m x n matrix and B to be a n x p matrix. Remember that you may multiply these two matrices to get a m x p matrix C. Only if the number of columns n in A equals the number of rows n in B is matrix multiplication feasible.

Remember: In matrix multiplication, the elements of the first matrix's rows are multiplied by the elements of the second matrix's columns.

The * operator is used in MATLAB to accomplish matrix multiplication.

Example: You can write and test the script mentioned below:

a = [ 1 2 3; 2 3 4; 1 2 5]

b = [ 2 1 3 ; 5 0 -2; 2 3 -1]

prod = a * b

When you write this codebase, you get the following output:

prod =

      18    10    -4

      27    14    -4

      22    16    -6

Determinant of a Matrix

The det function in MATLAB is used to determine the determinant of a matrix. Det is the determinant of matrix A.

Example:

Make a script file using the following code:

a = [ 1 2 3; 2 3 4; 1 2 5]

det(a)

When you write this codebase, you get the following output:

a =

      1     2     3

      2     3     4

      1     2     5

ans = -2

Matlab Matrix Inverse

A1 denotes the inverse of a matrix A such that the following connection holds:

AA−1 = A−1A = 1

A matrix's inverse does not necessarily exist. If the matrix's determinant is 0, the inverse does not exist, and the matrix is singular.

The inv function in MATLAB is used to determine the inverse of a matrix. inv is the inverse of a matrix A. 

a = [ 1 2 3; 2 3 4; 1 2 5]

inv(a)

When you write this codebase, you get the following output:

a =

   1     2     3

   2     3     4

   1     2     5

ans =

   -3.5000    2.0000    0.5000

   3.0000   -1.0000    -1.0000

   -0.5000      0       0.5000

FAQs

1. How can we create a matrix in MATLAB?

To make an array with three elements in a row, divide the elements with a comma (,)  or a space.

2. What is matrix command in MATLAB?

A scalar function that translates one matrix to another is known as a matrix command.

3. How do you enter a 3x3 matrix in MATLAB?

For example, as the first page of a 3-D array, define a 3-by-3 matrix. Add a second page now. Assign another 3-by-3 matrix to the index value 2 in the third dimension to do this.

4. What does matrix do in MATLAB?

The word matrix is used in the MATLAB environment to refer to a variable that contains real or complex values organised in a two-dimensional grid. In general, an array is a vector, matrix, or higher-dimensional grid of integers.

5. How do you write a matrix?

Divide the components using a comma (,) or a space to create an array with three elements in a row.

6. How do you write a variable matrix in MATLAB?

You need to know the concept of symbolic matrices to write a variable matrix in MATLAB.

Become a Data Scientist through hands-on learning with hackathons, masterclasses, webinars, and Ask-Me-Anything! Start learning now!

Conclusion

Addition and subtraction are simple in matrix arithmetic, but multiplication is difficult. MatLab simplifies the process since MatLab is specifically intended for matrix operations. MatLab supports all operations, including addition,  trigonometric functions, subtraction, multiplication, matrix transpose, cross multiplication, complex numbers, matrix inverse, and so on. You will quickly master all of these functions. Check out our Data Scientist Master’s Program to learn more about it.

Data Science & Business Analytics Courses Duration and Fees

Data Science & Business Analytics programs typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Post Graduate Program in Data Science

Cohort Starts: 22 Apr, 2024

11 Months$ 4,500
Post Graduate Program in Data Science

Cohort Starts: 6 May, 2024

11 Months$ 4,199
Post Graduate Program in Data Analytics

Cohort Starts: 6 May, 2024

8 Months$ 3,749
Applied AI & Data Science

Cohort Starts: 14 May, 2024

3 Months$ 2,624
Data Analytics Bootcamp

Cohort Starts: 24 Jun, 2024

6 Months$ 8,500
Data Scientist11 Months$ 1,449
Data Analyst11 Months$ 1,449