Matlab Matrix – Quick Guide

Matlab-Matrix – Quick Guide ”; Previous Next Matlab-Matrix – Introduction MATLAB (matrix laboratory) is a fourth-generation high-level programming language and interactive environment for numerical computation, visualization and programming. It allows matrix manipulations; plotting of functions and data; implementation of algorithms; creation of user interfaces; interfacing with programs written in other languages, including C, C++, Java, and FORTRAN; analyze data; develop algorithms; and create models and applications. In this tutorial we will focus on Matrix Implementation using MATLAB. Matrix A matrix is a collection of numbers arranged in rows and columns that represents a rectangular array. An example of matrix with 2 rows and 3 columns is as shown below Matrix Dimension The dimension of a matrix is defined based on the number of rows and columns. A matrix with 2 rows and 3 columns is said to be 2×3 matrix. A matrix with 3 rows and 3 columns is said to be 3×3 matrix. Matrix in Matlab In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers and using semicolons to mark the end of each row. Example To create a 4×5 matrix, enter the following. a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] The matrix has 4 rows and 5 columns. The first row will have values as 1 2 3 4 5 The second row: 2 3 4 5 6 The third row: 3 4 5 6 7 The fourth row: 4 5 6 7 8 Output The matrix of size 4×5 will look as follows a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 Let us test the matrix creation in MATLAB command window as shown below − >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> Referencing the Elements To reference an element in the mth row and nth column, of a matrix mx, we write the following mx(m, n); Example To refer to the element in the 2nd row and 5th column, of the matrix a, as created in the last section, we type the following. >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> a(2,5) ans = 6 >> To get all the elements of the nth column in a matrix , you can make use of A (:,n) where n represents the column no in the matrix. A(:,n). Example Now, let us create a column vector v, from all the elements of the 4th column of the matrix a. This will be as follows a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; v = a(:,4) Output MATLAB will execute the above statement and return the following result. >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> v=a(:,4) v = 4 5 6 7 >> You can also select the elements in the mth through nth columns. For this, we write as follows. a(:,m:n) Example Let us create a smaller matrix by taking the elements from the second and third columns, as shown below − a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(:, 2:3) Output MATLAB will execute the above statement and return the following result − >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> a(:, 2:3) ans = 2 3 3 4 4 5 5 6 >> In the same way, you can create a sub-matrix by taking a sub-part of a matrix. Example Let us create a sub-matrix saby taking the inner subpart of a, as given below − 3 4 5 4 5 6 During execution in MATLAB command window, the matrix will be as shown below − >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> sa = a(2:3,2:4) sa = 3 4 5 4 5 6 >> Matlab-Matrix – Environment Setup The official website of MATLAB is https://www.mathworks.com. The following page will appear on your screen − To download MATLAB go to https://in.mathworks.com/downloads/ as shown below − MATLAB is not free to download and you need to pay for the licensed

Matlab-Matrix – Inverse

Matlab-Matrix – Inverse ”; Previous Next The inverse of a matrix A is denoted by A−1 such that the following relationship holds − AA−1 = A−1A = 1 The inverse of a matrix does not always exist. If the determinant of the matrix is zero, then the inverse does not exist and the matrix is singular. Inverse of a matrix in MATLAB is calculated using the inv function. Inverse of a matrix A is given by inv(A). Example Here is an example to calculate inverse of given matrix − a = [ 1 2 3; 2 3 4; 1 2 5]; test = inv(a) Output The execution in MATLAB gives following result − >> a = [ 1 2 3; 2 3 4; 1 2 5]; test = inv(a) test = -3.5000 2.0000 0.5000 3.0000 -1.0000 -1.0000 -0.5000 0 0.5000 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Addition

Matlab-Matrix – Addition ”; Previous Next To add two matrices, both the operand matrices must have the same number of rows and columns. Example Here is an example a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = a + b Output On execution in MATLAB the result is as follows − >> a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = a + b c = 8 7 9 6 5 14 12 15 10 >> The plus() function You can also make use of plus() built-in function to add two matrices as shown below − Example Consider the following example for the use of plus() function to add the two matrices − a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = plus(a,b) Output The execution in MATLAB is as shown below − >> a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = plus(a,b) c = 8 7 9 6 5 14 12 15 10 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Trace

Matlab-Matrix – Trace ”; Previous Next Trace helps you to calculate the sum of diagonal elements in a given matrix. Example Consider the given 3×3 matrix. Let us find out the sum of diagonal elements as shown below − a = [ 1 2 3; 2 3 4; 1 2 5]; test = trace(a) Output The execution in MATLAB is as follows − >> a = [ 1 2 3; 2 3 4; 1 2 5] test = trace(a) a = 1 2 3 2 3 4 1 2 5 test = 9 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Subtraction

Matlab-Matrix – Subtraction ”; Previous Next To subtract two matrices, both the operand matrices must have the same number of rows and columns. Example Here is an example − a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = a – b Output On execution in MATLAB the result is as follows − >> a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = a – b c = -6 -3 -3 2 5 -2 2 1 8 >> The minus() Function You can also make use of the minus() built-in function to subtract two matrices. Example Consider the following example for use of minus() function for subtraction of two matrices − a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = minus(a , b) Output You will get the following result − >> a = [ 1 2 3 ; 4 5 6; 7 8 9]; b = [ 7 5 6 ; 2 0 8; 5 7 1]; c = minus(a , b) c = -6 -3 -3 2 5 -2 2 1 8 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Introduction

Matlab-Matrix – Introduction ”; Previous Next MATLAB (matrix laboratory) is a fourth-generation high-level programming language and interactive environment for numerical computation, visualization and programming. It allows matrix manipulations; plotting of functions and data; implementation of algorithms; creation of user interfaces; interfacing with programs written in other languages, including C, C++, Java, and FORTRAN; analyze data; develop algorithms; and create models and applications. In this tutorial we will focus on Matrix Implementation using MATLAB. Matrix A matrix is a collection of numbers arranged in rows and columns that represents a rectangular array. An example of matrix with 2 rows and 3 columns is as shown below Matrix Dimension The dimension of a matrix is defined based on the number of rows and columns. A matrix with 2 rows and 3 columns is said to be 2×3 matrix. A matrix with 3 rows and 3 columns is said to be 3×3 matrix. Matrix in Matlab In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers and using semicolons to mark the end of each row. Example To create a 4×5 matrix, enter the following. a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] The matrix has 4 rows and 5 columns. The first row will have values as 1 2 3 4 5 The second row: 2 3 4 5 6 The third row: 3 4 5 6 7 The fourth row: 4 5 6 7 8 Output The matrix of size 4×5 will look as follows a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 Let us test the matrix creation in MATLAB command window as shown below − >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> Referencing the Elements To reference an element in the mth row and nth column, of a matrix mx, we write the following mx(m, n); Example To refer to the element in the 2nd row and 5th column, of the matrix a, as created in the last section, we type the following. >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> a(2,5) ans = 6 >> To get all the elements of the nth column in a matrix , you can make use of A (:,n) where n represents the column no in the matrix. A(:,n). Example Now, let us create a column vector v, from all the elements of the 4th column of the matrix a. This will be as follows a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; v = a(:,4) Output MATLAB will execute the above statement and return the following result. >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> v=a(:,4) v = 4 5 6 7 >> You can also select the elements in the mth through nth columns. For this, we write as follows. a(:,m:n) Example Let us create a smaller matrix by taking the elements from the second and third columns, as shown below − a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]; a(:, 2:3) Output MATLAB will execute the above statement and return the following result − >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> a(:, 2:3) ans = 2 3 3 4 4 5 5 6 >> In the same way, you can create a sub-matrix by taking a sub-part of a matrix. Example Let us create a sub-matrix saby taking the inner subpart of a, as given below − 3 4 5 4 5 6 During execution in MATLAB command window, the matrix will be as shown below − >> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8] a = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 >> sa = a(2:3,2:4) sa = 3 4 5 4 5 6 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Transpose

Matlab-Matrix – Transpose ”; Previous Next The transpose operation switches the rows and columns in a matrix. It is represented by a single quote(”). Example Consider following example − a = [ 10 12 23 ; 14 8 6; 27 8 9] b = a” Output The execution in MATLAB gives the following output − >> a = [ 10 12 23 ; 14 8 6; 27 8 9] b = a” a = 10 12 23 14 8 6 27 8 9 b = 10 14 27 12 8 8 23 6 9 >> The transpose() function You can also make use of the transpose() function to get the transpose of a matrix. Example Consider the following example for use of transpose() function − a = [ 10 12 23 ; 14 8 6; 27 8 9] b = transpose(a) Output You will get the following output − >> a = [ 10 12 23 ; 14 8 6; 27 8 9] b = transpose(a) a = 10 12 23 14 8 6 27 8 9 b = 10 14 27 12 8 8 23 6 9 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Rank

Matlab-Matrix – Rank ”; Previous Next The rank of the matrix is the number of linearly independent columns in a matrix. The function rank() helps to return the rank of a given matrix. Example Consider following example for the use of rank() function for a matrix − a = [ 1 2 3; 2 3 4; 1 2 5] test = rank(a) Output The output in MATLAB on execution of the code is as follows − >> a = [ 1 2 3; 2 3 4; 1 2 5] test = rank(a) a = 1 2 3 2 3 4 1 2 5 test = 3 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Matrix Determinant

Matlab-Matrix – Determinant ”; Previous Next Determinant of a matrix is calculated by using the det function of MATLAB. For example, the determinant of a matrix A is given by det(A). Example Consider following example for calculating the determinant of a matrix − a = [ 1 2 3; 2 3 4; 1 2 5]; test = det(a) Output The code on execution in MATLAB is as follows − >> a = [ 1 2 3; 2 3 4; 1 2 5]; test = det(a) test = -2 >> Print Page Previous Next Advertisements ”;

Matlab-Matrix – Home

Matlab-Matrix Tutorial PDF Version Quick Guide Resources Job Search Discussion MATLAB is a programming language developed by MathWorks. It started out as a matrix programming language where linear algebra programming was simple. It can be run both under interactive sessions and as a batch job. This tutorial gives you aggressively a gentle introduction of MATLAB programming language. It is designed to give students fluency in MATLAB programming language. Problem-based MATLAB examples have been given in simple and easy way to make your learning fast and effective. Audience This tutorial has been prepared for the beginners to help them understand basic to advanced functionality of MATLAB. After completing this tutorial you will find yourself at a moderate level of expertise in using MATLAB from where you can take yourself to next levels. Prerequisites We assume you have a little knowledge of any computer programming and understand concepts like variables, constants, expression, statements, etc. If you have done programming in any other high-level programming language like C, C++ or Java, then it will be very much beneficial and learning MATLAB will be like a fun for you. Print Page Previous Next Advertisements ”;