Categories
3. Matrix

writematrix

Matlab provides different types of functionality to the user; the writematrix is also one kind of functionality provided by Matlab. The writematrix provides the ability to write the homogeneous array into a comma delimited text file. In which that filename is a workspace variable name and it has a .txt extension. Sometimes writematrix is not able to write the file name at that time it constructs as matrix.txt. As per our requirement, we can use the Matlab writematrix function for different purposes. By using the Matlab writematrix function we can perform the different operations as per the requirement of the user.

Syntax:

There are multiple syntaxes available for the Matlab writematrix as follows.

writematrix(variable name)
writematrix(variable name, file name)

Explanation:

In the first syntax, we use the writematrix function with a single parameter that is a variable name, here we write any name as a variable name. By using this syntax we can write the homogenous array into the comma delimited text file as per requirement and the extension of that file is .txt.

Similarly in the second syntax, we use the writematrix function with two different parameters such as variable name that we already discussed in the above syntax and file name, which means we can specify the file with .txt, .dat, .csv extension for delimited files. And some different file formats we can use in writematrix such as .xls, .xlsm, or .xlsx for Excel.

How writematrix works in Matlab?

Now let’s see how writematrix works in Matlab as follows.

Required input data:

Writematrix uses a specified matrix as input data.

Specified file name:

The filename should be a string or character vector and it depends on the location where we want to write the file. So it uses the different location format as follows.

  1. If our location is the current folder: In this location, we just need to write the file name.
  2. If our location is another folder: In this type, we need to write the path or we can also call it as the relative pathname of the folder where we want to store the file.
  3. If we need to store files on the remote location: In this type, we need to provide a URL or we need to provide the full path that contains the folder address.

If we provide the file name with an extension then writematrix functions determine the specified format of the file otherwise it uses comma-separated file format.

If the newly created file name exists in a folder then writematrix functions overwrite the file name.

Name-Value Pair for writematrix

This is an optional comma-separated pair of names and values, in which we can specify the multiple pair of name and value arguments as per the requirement. It contains the following options to write the file as follows.

  • Type of file:

It is used to specify the comma-separated file that contains the file name and string. In which that file name is used as input argument and it included the different extension such as .txt, .dat, ot.csv for the delimited text files and .xls, .xlsm or .xlsx for excel file format

  • WriteMode of file type:

In which we can specify the write mode of a comma-separated file that contains the pair of two values that is wrieMode and string and it depends on the file types as follows.

If we have a text file type then we use overwrite as the default write mode.

If we have a spreadsheet file type then we can use it in place as default write mode.

  • The delimiter in writematrix:

By using the delimiter field we can write the different delimiters as per our requirement such as Comma, Space, Tab, Semicolon, etc.

  • If we have spreadsheet file type:

In this file type, it contains a pair of sheets’ names and string arguments. Here we can assign the sheet name as per our requirement as well as we can also specify the index of the sheet. It uses different data types such as char, string, single, double and int8, etc.

Examples

Now let’s see the different examples of writematrix in Matlab for better understanding as follows.

Let’s see how we can write the matrix into the text file as follows.

First, we need to create the matrix. Here we use the magic function to create the n-by –n matrix as below.

A = magic(4)

Explanation:

By using the above statement we created a 4 by 4 matrix. As shown below screenshot as follows.

Matlab writematrix output 1

After that, we need to write this matrix into the text file by using the following statement as follows.

writematrix(A)
type 'A.txt'

Explanation:

In the above statement, we use the writematrix function, here A is the file name and we use the txt file type to create the text file of the matrix as shown. The final output of the above statement we illustrated by using the following screenshot as follows.

Matlab writematrix output 2

Now we can see data of A file as below mentioned screenshot as follows.

output 3

In the above example, we can use a different delimiter as per our requirements.

Now let’s see how we can write the matrix into the spreadsheet as follows.

First, we need to create the matrix. Here we use the magic function to create the n-by –n matrix as below.

A = magic(4)

Explanation:

By using the above statement we created a 4 by 4 matrix. As shown below screenshot as follows.

output 4

After that, we need to write this matrix into the spreadsheet file by using the following statement as follows.

writematrix(A,'A.xls')

Explanation

By using the above statement we write the matrix into the spreadsheet. The final output of the above statement we illustrated by using the following screenshot as follows.

Matlab writematrix output 5
readmatrix('A.xls')

Explanation:

By using the above statement we can read the content of the above-created spreadsheet.  The final output of the above statement we illustrated by using the following screenshot as follows.

output 6

In this way, we can perform the different operations by using the writematrix.

Categories
3. Matrix

Identity Matrix in Matlab

Matrix is defined as the arrangement of the numbers in rows and columns. They can be accessed with the help of row number and column number. If a matrix has 3 rows and 5 columns, then it is called a 3*5 matrix. There are different types of matrices, one of them is Identity Matrix. Identity Matrix is defined as the matrix where all the diagonal elements are ones and the rest of the elements are zeroes. It is also known as the elementary matrix or unit matrix. There are different operations that can be performed with identity matrix-like multiplication, addition, subtraction, etc.

Working of Identity Matrix in Matlab

In Matlab, the identity matrix is used for several purposes. It is denoted by I, E or U. The most important property of the identity matrix is, if a normal matrix is multiplied with the identity matrix then the resultant will always be the original matrix. Identity matrix can be of any dimension i.e. it can have any number of rows and columns. There are different operations that can be done with the help of the identity matrix in Matlab. In Matlab, the identity matrix can be created by using the” eye” keyword. We can define the dimension of the identity matrix by mentioning it in the brackets.

Below are the syntaxes which are used in Matlab to denote Identity Matrix:

  • U = eye: This syntax returns 1 of type scalar.
  • U= eye(a): This syntax returns identity matrix with a number of rows and a number of columns where all the diagonal elements are 1 and the remaining elements are zero.
  • U= eye (a, b): This syntax returns identity matrix with a number of rows and b number of columns where all the diagonal elements are 1 and the remaining elements are zero.
  • U = eye (size of array): This syntax is defined to return an array containing ones as the diagonal elements and zeroes elsewhere. The size of the array is defined as the size of an identity matrix. For example eye ([1,2]) will create an array of 1 by 2 where all the diagonal elements are one.
  • U = eye (__, name of the type): This returns the data type of the identity matrix. For example eye(2,’int4’): This returns a 2 by 2 matrix where the elements have 4-bit integers.
  • U = eye (__,’ like’, a): This syntax means that identity matrix has the same type containing diagonal elements as 1 and rest other elements as 0 which has the same type as that of a.

Examples to Implement Identity Matrix in Matlab

Below are the examples of identity matrix in Matlab:

Example 1:

The below example always return scalar type value.

Code:

U = eye (3)

Output:

Identity Matrix in Matlab eg1

Explanation: In the above example, we have just created a simple identity matrix in Matlab, by defining the dimension inside the brackets. Here the dimension is 3 which means that identity is created with 3 number of rows and 3 number of columns where all the diagonal elements are 1 and rest other elements are zero. The diagonal elements are (1,1), (2,2), (3,3) in the above example. If the dimension is mentioned as 0 in the above example, then it will create an empty matrix and if the dimension is given as any negative value, then it will result in 0. The data types that can be accepted are int8, int32, int16, single, double, etc.

Example 2:

To create an identity matrix with a number of rows and b number of columns.

Code:

U = eye (4,4)

Output:

Identity Matrix in Matlab eg2

Explanation: In the above example, we have given two dimensions to create an identity matrix which means it will create an identity matrix with a number of rows as 4 and number columns as 4 where all the diagonal elements are one and rest other elements as zero. The diagonal elements can be accessed by its row number and column number that are (1,1), (2,2), (3,3), (4,4). If the second part of the dimension is given as 0, then it will create an empty matrix and if the second part of the dimension is negative then it is always treated as zero. The data types that can be accepted are int8, int32, int16, single, double, etc.

Example 3:

To create an identity matrix by mentioning the size of an array.

Code:

sze = [4,1];
U = eye(sze)

Output:

Example 3

Explanation: In the above example, size is defined which will help in creating the identity matrix in Matlab. As the size is given as 4 by 1 array which means it will create an identity matrix with 4 numbers of rows and number of columns as 1. Size can have only two values and not more than that. If the size of the given element is mentioned as 0, then it will create an empty matrix and if the size of the given element is mentioned or declared as negative integer then it is always treated as 0. If the size is [4,5] then it will create a 4 by 5 matrix having a number of rows like 4 and the number of columns as 5.

The data types that can be accepted are int8, int32, int16, single, double, etc. The class or the type name can be int8, int32, int16, logical, single, double, etc. If we mention the output class of some prototype, then it also supports complex numbers. One of the most important properties of an identity matrix is that if we multiply a normal matrix with an identity matrix having the same dimension then the resultant matrix will always be the original matrix without any change in the elements. Please find the below example which will give you a better idea in understanding the concept behind it.

Example 4:

This is the other example that shows how to perform matrix multiplication using syntaxes.

A=52
34
B=10
01

Output:

A*B=5*1+2*05*0+2*1
3*1+4*03*0+4*1
52
34

Explanation: In the above example, if a normal matrix is multiplied with the identity matrix then the resultant is the normal matrix with no change in the values having the same dimension.

Categories
3. Matrix

Matrix Multiplication

‘Matlab’ word represents Matrix laboratory. Initially, Matlab designed for the implementation of matrix operations. By using Matlab we can easily implement complex operations ad problems very easily. As we know in matrix operations multiplication is one of the difficult and complicated operations but by using simple command ‘mtimes’ we multiply two matrices.

There are some rules of matrix multiplication just like mathematics. If there are two matrices then a number of columns of the first matrix should be equal to the number of rows of the second column. Let us assume first matrix dimensions are 2 rows and 3 columns and second matrix dimensions are 4 rows and 3 columns then we cannot perform multiplication because a number of columns in the first matrix and number of rows in the second matrix are not the same.

How to Perform Matrix Multiplication in Matlab?

There are two ways to multiply matrix one is by using multiplication ‘*’ operator. And second is by using ‘mtimes command.

Using ‘ * ’ Operator

To multiply two matrices first we need two matrix. we can directly declare the matrices or we can accept input from the user. Here are some of the steps that we need to follow as given below:

  • Step 1: accept two matrix by declaring two variables.
  • Step 2: assign 3rd variable for output and write a statement as matrix 1 * matrix 2.
  • Step 3: display output.

Using ‘mtimes’ Command

In this method, there is no need for operators we can give the direct command to the input matrix. A statement can be written as mtimes ( matrix 1, matrix 2 )

  • Step 1: accept two matrix by declaring two variables.
  • Step 2: assign a 3rd variable for output and give command mtimes.
  • Step 3: display output.

Examples to Implement Matrix Multiplication

Here are some of the examples of matrix multiplication in Matlab which are given below:

Example 1:

Let us consider two matrix mat1 and mat2,

mat 1 =

1     2     3

3     4     2

3     2     1

mat 2 =

1     1     1

3     4     2

3     2     1

Using ‘ * ’ Operator

The following table shows the above example by using the ‘ * ’ operator.

 Matlab editorOutput
mat1 = [ 1 2 3 ; 3 4 2 ; 3 2 1 ]mat2=[ 1 1 1; 3 4 2 ; 3 2 1 ]mat3 = mat1 * mat2mat1 =1     2     33     4     23     2     1mat2 =1     1     13     4     23     2     1mat3 =16    15     821    23    1312    13     8

Output:

Matrix Multiplication in Matlab eg1

Using ‘mtimes’ Command

The following table shows the above example by using the ‘mtimes’ command.

Matlab EditorOutput
mat1= [ 1 2 3 ; 3 4 2 ; 3 2 1 ]mat2=[ 1 1 1 ; 3 4 2 ; 3 2 1 ]mat4= mtimes ( mat1 , mat2 )mat1 =1     2     33     4     23     2     1mat2 =1     1     13     4     23     2     1mat4 =16    15     821    23    1312    13     8

Output:

Matrix Multiplication in Matlab eg2

Example 2:

Let us consider two matrix mat and mat 2 are,

mat1 =

23    32    11

22     3     2

16    39    21

32     4     1

mat2 =

41    11    43

32    41    32

3     2     1

In the above example, dimensions of the first matrix are 4 rows and 3 columns, and dimensions of the second matrix are 3 rows and 3 columns so the number of columns of the first matrix is equal to the number of rows of the second matrix so multiplication can be executed.

Using ‘ * ’ Operator

The following table shows the above example by using the ‘ * ’ operator.

Matlab editorOutput
mat1 = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]mat2 = [ 4 1 11 43 ; 32 41 32 ; 3 2 1 ]mat3 = mat1 * mat 2mat1 =23    32    1122     3     216    39    2132     4     1mat2 =41    11    4332    41    323     2     1mat3 =2000        1587        20241004         369        10441967        1817        19571443         518        1505

Output:

Matrix Multiplication in Matlab eg2.1

Using ‘mtimes’ Command

The following table shows the above example by using ‘mtimes’ command.

Matlab editorOutput
mat1 = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]mat2 = [ 41 11 43 ; 32 41 32 ; 3 2 1 ]mat4 = mtimes ( mat1 , mat2 )mat1 =23    32    1122     3     216    39    2132     4     1mat2 =41    11    4332    41    323     2     1mat4 =2000        1587        20241004         369        10441967        1817        19571443         518        1505

Output:

eg2.2

Example 3:

Let us assume two matrices are mat1 and mat2,

mat1 =

5     6     3     2

3     2     4     5

3     2     1     1

mat2 =

3     4     2

2     3     4

3     3     4

In the above example, the dimension of the first matrix are 3 rows and 4 columns and dimensions of the second matrix are 3 rows and 3 columns so a number of columns of the first matrix are not equal to the number of rows of the second matrix so multiplication cannot execute.

Matlab editorOutput
mat1= [ 5 6 3 2 ; 3 2 4 5 ; 3 2 1 1 ]mat2= [ 3 4 2 ; 2 3 4 ; 3 3 4 ]mat3= mat1 * mat2mat1 =5     6     3     23     2     4     53     2     1     1mat2 =3     4     22     3     43     3     4Error using *Inner matrix dimensions must agree.

Output:

eg3
Categories
3. Matrix

Sparse Matrix

Sparse Matrix in MATLAB is meant for storing the data which has zeroes and non-zeroes value so it saves memory and helps in managing the data effectively. Suppose a matrix with x number of columns and y number of rows has less number of non-zero values as compared to the number of zeroes in that matrix, then it is known as Sparse matrix. Briefly, it is the matrix that contains less number of non-zero values. Sparse Matrix is used to reduce the scanning time and saves a lot of space.

There are 2 types of Sparse Matrix operations used in Matlab:

  • Computational Complexity: It is equal to the number of non-zero elements present in a sparse matrix. It is also dependent on its row and column size but it is independent on the product i.e. the number of elements present in a sparse matrix including both zero and non-zero elements.
  • Algorithmic Details: There are various rules that sparse matrix should follow depending on the functions and operators used

Creation and Working with Sparse Matrix in MATLAB?

Below are the creation and working with Sparse Matrix:

  • Suppose we have 10*10 matrix which has only 5 non zero values and the rest other are zeroes. Generally, we require 10*10*2=40 bytes of memory to store this 2-dimensional matrix and to access the 5 non-zero elements present in a matrix we have to scan around 100 times which is not feasible and very time-consuming.
  • So, to overcome this issue we can use sparse matrix representation. Generally, there are two types of representation used in the sparse matrix which are Triplet Representation and Linked Representation.
  • In Matlab, we can create a sparse matrix by using the keyword “sparse”.

The syntax which is used to represent the sparse matrix in Matlab with additional features like:

i = Sparse(M)
  • This is used to convert a normal matrix M to the sparse matrix which will squeeze out the zeroes present in the matrix and it helps in saving the memory. If a matrix contains many zeroes, then it does not automatically convert the matrix to the sparse matrix. We have to consider the density of the matrix to determine if we should convert the matrix to a sparse matrix or not.
  • The density of a matrix is defined as the total number of non-zero elements present in the matrix divided by the total number of values present in the matrix. Generally, a matrix that has low density is considered a better fit to convert it into a sparse matrix.

Below are the examples of Sparse Matrix in MATLAB:

Example 1:

To sparse a matrix if it has repeated values.

a = [5,5,5,6,9,9,10,10] b= [2,2,2,1,3,3,9,9] c= [200,300,100,400,500,600,700,800] [a, b, c]
Sparse Matrix
Sparse Matrix

In the above example, if there are repeated elements for ath and bth value then the respective values for them are added and the resultant value is assigned. For example: in the case of 5 and 2 pairs, the values are added and the sum is assigned. If the matrix has such type of format, then it can be sparsed in the same way as shown in the above example.

The input matrix can be of the double and logical data types. It can also handle complex numbers. Sparse Matrix is meant to store only the elements that are not zero in a matrix which saves the memory and reduces the computational time. The space required by the elements does not depend on the type whether it is zero or non –zero. It will allocate the same amount of memory location to zero and non-zero elements.

If a matrix is large and it has many zeroes, then this reduces the memory required for storing the data. Sparse Matrices not only reduce the memory storage but also reduce the execution time. If we convert the normal into a sparse matrix, then unnecessary operations are not performed like low-level arithmetic calculations are not performed in the sparse matrix which further reduces the execution time of the calculation and provides the results. If it has huge data, then it is really helpful in executing any expression.

Example 2:

To convert a full matrix to sparse matrix:

M= ([2  5  4  1  3 ] [ 4  5  1  2  3 ] [1  2   3  4  5])
full matrix

In the above example, the first element is taken from both the list and combined with their indices. We can also import different sparse matrix from outside using different functions in Matlab.

There are various ways of processing and accessing the sparse matrix in Matlab like:

  • nonzeros: It returns all the non-zero elements present in a sparse matrix resulting in a column vector.
  • nnz: It returns the count of the values that are not zero present in a sparse matrix.
  • nzmax: It returns the memory size that is assigned to the elements which are not zero present in the sparse matrix.

Find function in Matlab is used to find the elements that are not zero, row and column indices of a sparse matrix. We can also insert or append an additional row in a matrix as shown in the below example:

Example 3:

C = 1   2     1
3   2      2
4   5      2
5    2     1
C (3,1) = 52
[m, n, a] = find(C);
[m, n, a]
additional row in a matrix

Here we are inserting a new row into the matrix and the other rows are shifted subsequently after adding the additional row. We can also visualize the non-zero elements of a sparse matrix which shows the distribution of non-zero elements present in the matrix. The function which is used to view the non-zero elements of the sparse matrix is the “spy” function, where each point present in the graph represents the location of each element which is not zero in a sparse matrix.

Categories
3. Matrix

Transpose Matrix

In this article, we will learn about Transpose Matrix Matlab. If output matrix rows are equal to input columns and output matrix columns are equal to rows of the input matrix then the output matrix is called ‘transpose of the matrix’. This ‘T’ represents the transpose of the matrix. Let us consider there are two matrices one is the input matrix ‘I’ and the second is the output matrix ‘O’. A number of rows of the input matrix are ‘Irow’. The number of columns in the input matrix is ‘Idol’ and the number of rows in the output matrix is ‘Orow’. Number of columns in output matrix is ‘Ocol’ then Transpose of matrix satisfies two conditions which are Icol=Orow and Irow=Ocol.(rows of input matrix =column of output matrix and columns of input matrix=rows of output matrix)

How We Can Do Transpose Matrix in Matlab?

There are two ways to find out transpose of a matrix in Matlab which are:

  1. By Using Operator
  2. By Using Command

1. By Using Operator

In this method, the dot operator is used to finding the transpose of the matrix (. ’). This is one of the easiest and simple methods for transpose. The only limit of this method is there are high chances of syntax error because of the operator.

Syntax:

Output matrix=input matrix . ’

Steps:

  • Accept input matrix by using square matrix (Input = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]
  • Apply the operator on the input matrix ( output matrix=input matrix.’)
  • Display the output matrix.

2. By Using Command

In this method, ‘transpose’ command is used to find out the transpose of the matrix. This method is less complex and easy to implement as compared to the previous method. And there are fewer chances of error at the time of implementation.

Syntax:

Output matrix=transpose (input matrix)

Steps:

  • Accept input matrix by using square matrix (Input = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]
  • Apply command on input matrix ( output matrix = transpose (input matrix))
  • Display output matrix.

Examples for Transpose Matrix Matlab

Following are the examples implement the matrix Matlab:

Example 1:

Let us consider the input matrix as mat1;


Code:

mat1 =
23 32  11
22   3   2
16  39  21
32   4   1


The following table illustrates the Matlab code for example 1 by using the operator.

Matlab EditorCommand Window(output)
mat1 = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ] mat3 = mat1 . ‘mat1 =23    32    1122     3     216    39    2132     4     1mat3 =23    22    16    3232     3    39     411     2    21     1

Output:

using the operator

The following table illustrate the Matlab code for example 1 by using transpose command:

Matlab EditorCommand Window(output)
mat1 = [ 23 , 32 , 11 ; 22 3 2 ; 16 39 21 ; 32 4 1 ]mat2=transpose(mat1)mat1 =23    32    1122     3     216    39    2132     4     1mat2 =23    22    16    3232     3    39     411     2    21     1
Transpose Matrix Matlab - 2

Example 2:

Let us assume input matrix is mat1 in form of the magic command.

mat1 =
16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1

The following table illustrates the Matlab code for example 1 by using a transpose operator.

Matlab EditorCommand Window (Output)
mat1 = magic(4)mat2 = mat1.’mat1 =16     2     3    135    11    10     89     7     6    124    14    15     1mat2 =16     5     9     42    11     7    143    10     6    1513     8    12     1
transpose operator

Following table illustrate the Matlab code for example 2 by using transpose command.

Matlab EditorCommand Window ( Output )
mat1 = magic ( 4 )%no 4 represents no of rows and columns%mat2=transpose(mat1)mat1 =16     2     3    135    11    10     89     7     6    124    14    15     1mat2 =16     5     9     42    11     7    143    10     6    1513     8    12     1
transpose command

Example 3:

Now consider one input matrix with complex numbers.

mat1 =
1.0000 - 3.0000 i     4.0000 + 4.0000 i     8.0000 + 0.0000 i     2.0000 - 5.0000 i
5.0000 + 0.0000 i     3.0000 + 0.0000 i     6.0000 + 8.0000 i     4.0000 + 4.0000 i
0.0000 + 1.0000 i     7.0000 + 8.0000 i     9.0000 + 9.0000 i     10.0000 + 0.0000 i
Matlab EditorCommand Window ( Output )
mat1 = [ 1 – 3 i , 4  + 4 i , 8 , 2 – 5 i ; 5  , 3 , 6 + 8 i , 4 + 4 i ; i , 7 + 8 i , 9 + 9 i , 10]mat2=mat1.’mat1 =1.0000 – 3.0000i   4.0000 + 4.0000i   8.0000 + 0.0000i   2.0000 – 5.0000i5.0000 + 0.0000i   3.0000 + 0.0000i   6.0000 + 8.0000i   4.0000 + 4.0000i0.0000 + 1.0000i   7.0000 + 8.0000i   9.0000 + 9.0000i  10.0000 + 0.0000imat2 =1.0000 – 3.0000i   5.0000 + 0.0000i   0.0000 + 1.0000i4.0000 + 4.0000i   3.0000 + 0.0000i   7.0000 + 8.0000i8.0000 + 0.0000i   6.0000 + 8.0000i   9.0000 + 9.0000i2.0000 – 5.0000i   4.0000 + 4.0000i  10.0000 + 0.0000i
 complex numbers
Matlab EditorCommand Window ( Output )
mat1=[1-3i,4+4i,8,2-5i;5,3,6+8i,4+4i;i,7+8i,9+9i,10]mat2=transpose(mat1)mat1 =1.0000 – 3.0000i   4.0000 + 4.0000i   8.0000 + 0.0000i   2.0000 – 5.0000i5.0000 + 0.0000i   3.0000 + 0.0000i   6.0000 + 8.0000i   4.0000 + 4.0000i0.0000 + 1.0000i   7.0000 + 8.0000i   9.0000 + 9.0000i  10.0000 + 0.0000imat2 =1.0000 – 3.0000i   5.0000 + 0.0000i   0.0000 + 1.0000i4.0000 + 4.0000i   3.0000 + 0.0000i   7.0000 + 8.0000i8.0000 + 0.0000i   6.0000 + 8.0000i   9.0000 + 9.0000i2.0000 – 5.0000i   4.0000 + 4.0000i  10.0000 + 0.0000i
Transpose Matrix Matlab - 6
Categories
3. Matrix

3D Matrix

MATLAB is a language used for technical computing. As most of us will agree, an easy-to-use environment is a must for integrating computing, visualizing, and finally programming tasks. MATLAB does the same by providing an environment that is easy to use and the solutions that we get are displayed in terms of mathematical notations, which most of us are familiar with. In this topic, we are going to learn about 3D Matrix in MATLAB.

Uses of MATLAB Include

  • Computation
  • Development of Algorithms
  • Modeling
  • Simulation
  • Prototyping
  • Data analytics (Analysis and Visualization of data)
  • Engineering & Scientific graphics
  • Application development

In this article, we will understand multidimensional arrays in MATLAB and, more specifically, 3- dimensional Matrix in Matlab.

Multidimensional array

It is an array in MATLAB which has two or more dimensions. You might be already knowing that the dimensions of a 2D matrix are represented by rows and columns.

Multidimensional array

Each element has two subscripts one is the row index and the other is the column index.

e.g. (1,1) element here represents Row number is 1 and the column number is 1.

What is a 3-D Matrix?

3-D Matrix is a multidimensional array that is an extension of two-dimensional matrices. As you can guess, they will have 3 subscripts, one subscript along with row and column indexes as for the 2D matrix. The third subscript in a 3D Matrix is used to represent the sheets or pages of an element.

e.g. Here element (2,1,1) represents ‘Row’ number 2 ‘Column’ number one and ‘Page’ number 1.

Creation of 3D Matrix

Let’s now understand how can we create a 3D Matrix in MATLAB

For a 3-dimensional array, create a 2D matrix first and then extend it to a 3D matrix.

  • Create a 3 by 3 matrix as the first page in a 3-D array (you can clearly see that we are first creating a 2D matrix)
A = [11 2 7; 4 1 0; 7 1 5]
  • Add a second page now. This can be done by assigning one more 3 by 3 matrix with index value 2 in the third dimension

A(: , :, 2) =  [1 2 5 ; 4 4 6 ; 2 8 1]

A[3×3]

A =

A(:,:,1)=1127
410
715
A(:,:,2) =125
446
281

We can also use a function called cat Function to create multidimensional arrays.

For Example: Create a 3D array with 3 pages using cat function

X = cat(3,A,[3 7 1; 0 1 8; 2 5 4])
  • Here A is the 3D array created above
  • Argument at first place (3) tells which direction the array needs to be concatenated
  • Here concatenation is being done along with the pages

X=

X(:,:,1) =1127
410
715
X(:,:,2) =123
446
281
X(:,:,3) =371
018
254

Now, if we need to further expand this array, we can simply give the elements of 4th array that we need to add:

So to extend our above example, we will simply give,

B(:,:,4) = [1 2 1; 3 9 1; 6 3 7] and output will be:

X=

X(:,:,1) =1127
410
715
X(:,:,2) =123
446
281
X(:,:,3) =371
018
254
X(:,:,4) =121
391
637

How can we access the elements of the array?

To do this simply use subscripts as integers. So, 2,3,1 element of a 3D Matrix will be the element present at 2nd row, 3rd column of the 1st page

To demonstrate this, let’s use the 3D matrix A which we used above,

Now, access = A(2,3,1) will give us 0 as output

Functions to manipulate the elements of a Multidimensional Array

MATLAB provides us with a couple of functions to manipulate the elements of a multidimensional array.

  • Reshape
  • Permute

Let’s understand these ones by one:

1. Reshape

This is useful mainly during visualization of data

For Example: Create a 6*5 matrics using two 3*5 matrices

  • A = [1 3 7 0 5; 2 0 4 1 3; 1 0 5 3 2];
  • A(:,:,2) = [1 7 2 5 0; 4 2 1 6 5; 1 1 4 5 0];
  • B = reshape(A,[6 5])

This will create a 2D matrix with 6 rows and 5 columns:

B = 6×5

1     7     5     7     5

2     4     3     2     6

1     5     2     1     5

3     0     1     2     0

0     1     4     1     5

0     3     1     4     0

As you can notice, RESHAPE will work column-wise, so first all the elements of A take along the column, for the first page. The same thing is then done for 2nd page

2. Permute

We can use this function if we want to rearrange the dimensions of the matrics. i.e., changing rows with columns or vice versa.

Example of Permute:

  • P(:,:,1) = [3 5 3; 1 5 2; 0 8 5];
  • P(:,:,2) = [0 1 3; 6 7 1; 4 2 1]

Let’s now use PERMUTE function on P:

  • M = permute(P,[2 1 3])

The output that we will get will have rows and columns interchanged as follows:

M1 =

M1(:,:,1) =310
558
325
P1(:,:,2) =064
172
311
Categories
3. Matrix

Matrix in Matlab

Matrix in Matlab is a type of variable that is used for mathematical computation purposes. Matlab is known as Matrix Laboratory that efficiently processes matrix calculations. Matrix is a two-dimensional array that is part of linear algebra associated with analytics. Matlab provides inbuilt functionality for creating the matrix and assigning the values to it. There are several mathematical and trigonometric computations supported by Matlab software. Some of the arithmetic operations on the matrix in Matlab are addition, subtraction, multiplication. Similarly, it supports tan, cos, sin, cosec, sec, cot, sin inverse operations. Also operations like complex numbers computation and concatenation operations for two matrix values.

Matrix Formation

  • First, we will see how to create an array in Matlab. An array is a row vector, so to create array commands will be X = [ 1 4 7 6 ]
  • In above example, there are four elements in one row. And array name is ‘ x ’.
  • An array is a one-dimensional quantity. To create matrix we need to specify a two-dimensional array, let us consider one example Matrix A is
Matrix 2

 To create the above matrix in MatLab commands will be

A = [ 4 5 6 ; 2 1 7 ; 4 0 3 ]

  • In this elements are written in square brackets ( ‘ [ ] ’ ) and each row separated by semicolon ( ‘ ; ’ ) .
  • Screen 1 shows the formation of a matrix that is an illustration of above example.
matrix - 1

Screen 1: Matrix in Matlab

  • Another way is to create a matrix is by using commands zeros, ones, etc.

Example : a=zeros(4,1)

A= 0

0

0

0

  • Inside the brackets, 4 means 4 rows and 1 is a number of a column.

a=ones(2,3) … … … Two rows and three columns.

Matrix 3

Ouput:

matrix - 2

Screen 2: Matrix in Matlab

Operations on Matrix

Below are the different operations on matrix:

1. Arithmetic Operation

It allows all arithmetic operations on a matrix such as addition, multiplication, subtraction, etc

Syntax:matrix name   operator   arithmetic constant

Example:

If a is  4 by 4 matrix with values

4 7 3

4 2 7

8 7 2

4 2 1

In Matlab it will be represented as  a = [ 4 7 3 ; 4 2 7 ; 8 7 2 ; 4 2 1 ]

a + 10

It will give output as

14  17  13

14  12  17

18  17  12

14  12  11

For

a  – 2

Output will be

2  5  1

2  0  5

6  5  0

2  0 -1

Above example shown on screen 3

Arithmetic operations

Screen 3: Arithmetic operations

2. Trigonometric Operations

In this, we can use all trigonometric operators like sin, cos, tan, cosec, sec, cot, sin inverse, etc

Consider one matrix B.

B =5  6  4

3  2  8

Matlab program will be

B = [ 5 6 4 ; 3 2 8 ]

sin ( B )

cos (B )

Output is

Trigonometric operations

Screen 4: Trigonometric Operations

3. Transpose of Matrix

To find the transpose of the matrix a single quote ( ‘  ) is used.

Let us consider matrix X =

Matrix 4

By applying command X ’

It will give transpose output as

Matrix 5

Above example illustrated in screen 5

Transpose of matrix

Screen 5: Transpose of Matrix

4. Matrix Multiplication

We can perform matrix multiplication. By using the multiplication operator we can multiply two matrices.

Let us consider X is

6      7    3    2

7      5    3   1

And transpose of X is

6             7

7             5

3             3

2             1

Matrix multiplication is given in screen 6.

multiplication

Screen 6 : Multiplication of Matrix

5. Power

To find power of any variable dot operator ( ‘ . ‘ ) is used  before power operator ,Let us consider Matrix X =  [ 6 7 3 2 ; 7 5 3 1 ]

X . ^ 3  =

216  343  27  8

343  125  27  1

6. Concatenation

Concatenation is used to join two matrix together , square brackets [ ] are used for concatenation operator.

Let us consider one example Matrix A  is

4 2

5 7

B= [A,A]

Output will be B

4 2 4 2

5 7 5 7

7. Complex Numbers

Complex numbers are a mixture of two parts. Real part and imaginary parts, generally to represent imaginary part  ‘ I ’ and  ‘ j ’ variable is used.

If we put square root operation in MatLab command window ( sqrt ( -1 ) ) then it gives output as 0.0000 + 1.0000 i

Here 0 is the real part and 1 is an imaginary part.

Complex numbers representation is as follows ;

A = [ 5 + 3 i , 5 ; 2 + 2 i , 3 + 1 i ]

It is 2 by 2 matrix, the output will be

5 + 3 i     5

2 + 2 i    3 + i

Above example illustrated in screen 7

Complex numbers

Screen 7: Complex Numbers

8. Size

This command is used to find the size of the matrix. It gives the size in the form of rows and columns. (number of rows and number of columns).

Let us consider example A = [  5 6 8 2 ; 6 5 4 3 ; 8 7 2 2 ]

Output for size (A) will be 3 4

Here 3 represents no of rows and 4 represents no of columns.

Size of Matrix

Screen 8: Size of Matrix