Matlab Matrix – Discussion

Discuss Matlab-Matrix ”; Previous Next 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. Print Page Previous Next Advertisements ”;

Matlab-Matrix – Multiplication

Matlab-Matrix – Multiplication ”; Previous Next Consider two matrices A and B. If A is an m x n matrix and B is an n x p matrix, they could be multiplied together to produce an m x n matrix C. Matrix multiplication is possible only if the number of columns n in A is equal to the number of rows n in B. In matrix multiplication, the elements of the rows in the first matrix are multiplied with the corresponding columns in the second matrix. Each element in the (i, j)thposition, in the resulting matrix C, is the summation of the products of elements in ith row of the first matrix with the corresponding element in the jth column of the second matrix. Matrix multiplication in MATLAB is performed by using the * operator. Example Consider following example in MATLAB a = [ 1 2 3; 2 3 4; 1 2 5]; b = [ 2 1 3 ; 5 0 -2; 2 3 -1]; prod = a * b Output The execution in MATLAB will display the following result − >> a = [ 1 2 3; 2 3 4; 1 2 5]; b = [ 2 1 3 ; 5 0 -2; 2 3 -1]; prod = a * b prod = 18 10 -4 27 14 -4 22 16 -6 >> The mtimes function You can also make use of the function mtimes to multiply two given matrices. It is a builtin function available in MATLAB. Example Consider following example − a = [ 1 2 3; 2 3 4; 1 2 5]; b = [ 2 1 3 ; 5 0 -2; 2 3 -1]; test= mtimes(a,b) Output On execution in MATLAB the output is as follows − >> a = [ 1 2 3; 2 3 4; 1 2 5]; b = [ 2 1 3 ; 5 0 -2; 2 3 -1]; test= mtimes(a,b) test = 18 10 -4 27 14 -4 22 16 -6 >> Print Page Previous Next Advertisements ”;

Matlab Matrix – Useful Resources

Matlab-Matrix – Useful Resources ”; Previous Next The following resources contain additional information on Matlab-Matrix. Please use them to get more in-depth knowledge on this. Useful Links on Matlab-Matrix Matlab-Matrix Documentation − Matlab-Matrix”s official website for its latest docs and updates. Useful Books on Matlab-Matrix To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Matlab-Matrix – Working with Matrices

Matlab-Matrix – Working with Matrices ”; Previous Next In this chapter, I will deal with how to run the matrix inside MATLAB Environment to get the output. To define a matrix, and other matrix operations are discussed in details in the next chapters. To get the matrix output in MATLAB will make use of − Command Prompt Using m-file Using Command Prompt You can execute matrices directly inside command prompt. Here is an example wherein we have two matrices a and b. The operation a+b gives the sum of matrix a and b. The operation a-b gives the subtraction of matrix a and b. The output is command prompt 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 = a + b c = 8 7 9 6 5 14 12 15 10 >> d = a – b d = -6 -3 -3 2 5 -2 2 1 8 >> Using m-file You can also make use of a file to write the code and later execute it inside command prompt as shown below − Click on New script as shown below − This is open a new unsaved file as shown below − Save the file and write your code inside The file is saved as testmatrix.m. Now you can use the run button or type the name of the file inside command window. The output will be shown in the command window as shown below − >> testmatrix c = 8 7 9 6 5 14 12 15 10 d = -6 -3 -3 2 5 -2 2 1 8 >> Print Page Previous Next Advertisements ”;

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 ”;