Matrix in MATLAB

Source: Internet
Author: User
Matrix in MATLAB we know that solving linear equations is the core content of linear algebra, and the matrix plays an important role in solving linear equations. Next we will use the scientific computing software MATLAB to demonstrate how to use the matrix, while also making the students' understanding of linear algebra more rational. 1. Matrix construction in MATLAB, there are two methods to construct a matrix. One is the direct method, which is to directly construct the matrix through keyboard input. The other is to use functions to generate matrices. Example 1. use the Pascal function to generate a matrix A = Pascal (3) A = 1 1 1 1 2 3 3 6 Example 2. use the magic function to generate a matrix B = magic (3) B = 8 1 6 3 5 4 9 2 example 3. the function can also be used to generate a 4*3 random matrix> C = rand (0.9501) C = 0.8913 0.8214 0.2311 0.7621 0.4447 0.6068 0.4565 0.61540.4860 0.0185 0.7919 Example 4. using the direct input method, you can generate the column matrix, row matrix, and constant u = [3; 1; 4] U = 3 1 4 V = [2 0-1] V = 2 0-1 s = 7 S = 7 2. Basic matrix operations 1. Arithmetic Operations 4. Example 5. addition of matrix X = a + bx = 9 2 74 7 105 12 8 Example 6. matrix subtraction y = X-AY = 8 1 6 3 5 7 4 9 2 Note: if the size of the two matrices is not exactly the same, it will error! For example, x = a + u ??? Error Using => plusmatrix dimensions must agree. Example 7. matrix Multiplication x = A * BX = 15 15 1526 38 2641 70 39 Note: if the number of columns in the first matrix is different from the number of rows in the second matrix, the two matrices cannot be multiplied. For example, x = A * V ??? Error Using ==> mtimesinner matrix dimensions must agree. In Matlab, the division of a matrix has two operators: The left division "/" and the right division "/". The right division of a matrix is slower, the left division operation can avoid the influence of singular matrices. They are mainly used to solve linear equations, and division of matrices will be involved later. 2. The transpose, inverse, and determining operations of a matrix are the same as those in linear algebra. The transpose of a matrix only needs to be represented by the symbol. Example 8. returns the transpose x = B 'x = 8 3 4 1 5 9 6 7 2 linear algebra of matrix B. in MATLAB, the Inverse Calculation of the matrix only requires the function "inv", which greatly simplifies the calculation process. Example 9. calculate the inverse x = inv (a) x = 3-3 1-3 5-21-2 1 of matrix A in MATLAB, and obtain the size of the determining factor of the matrix, it can be implemented using the "det" function. Example 10. Obtain the determinant x = det (a) x = 1 of matrix A. Note: When finding the inverse and determining of a matrix, the matrix must be a square matrix. Otherwise, an error will occur! For example,> X = inv (u )??? Error Using ==> invmatrix must be square. Another example is X = det (u )??? Error Using => detmatrix must be square. Iii. Common Matrix Function operations 1. in linear algebra, the process of calculating the matrix feature value and feature vector is quite troublesome. However, in MATLAB, the matrix feature value operation only requires the function "EIG" or "eigs. Example 11. evaluate the feature values and feature vectors of matrix A> [B, c] = EIG () B =-0.5438-0.8165 0.1938 0.7812-0.4082 0.4722-0.3065 0.4082 0.8599c = 0.1270 0 0 0 1.0000 0 0 7.8730 in the above example, the B and C matrices are feature vector matrices and Feature Matrix, respectively. 2. rank Calculation of matrices the rank Calculation of matrices is widely used in solving linear equations, while the rank Calculation of matrices in linear algebra is also very complex. However, in MATLAB, the rank of the matrix only needs to use the "rank" function. Example 12. rank> X = rank (a) x = 33 of matrix. the orthogonal operation of the matrix is in MATLAB, And the orthogonal operation of the matrix can be calculated by the function "Orth. The following example is used to obtain a group of orthogonal bases of a matrix. With the orthogonal basis, the matrix can be orthogonal. Example 13. evaluate the orthogonal basis> X = Orth (a) x =-0.1938 0.8165 0.5438-0.4722 0.4082-0.7812-0.8599-0.4082 0 of matrix. 30654. the trace operation of a matrix indicates the sum of all elements on the main diagonal of a matrix. in MATLAB, the trace of a matrix can be calculated by the "trace" function. Example 14. find the trace of matrix A> X = trace (a) x = 9 4. Several special matrices are provided in MATLAB for the generation of special matrices, including: 1. the null matrix is represented by "[]". The size of the null matrix is zero, but the variable name exists in the workspace. Example 15> [] ans = [] 2. in Matlab, the unit matrix can be implemented using the "eye (n, m)" function, where n table rows and M table columns. Example 16> X = eye () x = 1 0 0 1 0 0 1 0 0 0 0 03. in Matlab, a matrix with all elements 1 can be implemented using the "ones (n, m)" function. Example 17> X = ones (4, 3) x = 1 1 1 1 1 1 1 11 1 14. in Matlab, a matrix with all elements 0 can be implemented using the "zeros (n, m)" function. Example 18> X = zeros (4, 3) x = 0 0 0 0 0 0 00 0 05. cube matrix cube matrix has an interesting property. Each row, column, and two diagonal lines have the same elements. MATLAB provides the magic (n) function for finding the cube matrix, which is used to generate an N-level cube array. 6. In Matlab, the adjoint matrix of a matrix can be implemented using the "compan (a)" function. Example 20> U = [1 0-7 6];> X = compan (u) x = 0 7-6 1 0 00 1 0 Note: function compan () the variable must be in the vector form, rather than the matrix. 7. Random matrix random matrix is very important in the study of mathematical statistics. They indicate that elements obey a matrix with a certain distribution, such as uniform distribution and normal distribution. In Matlab, the random matrix can be implemented using the "RAND (n, m)" function. Example 21> X = rand (0.9501) x = 0.8913 0.8214 0.2311 0.7621 0.4447 0.6068 0.61540.4860 0.4565 0. 79198. pascal matrix we know that the coefficient after quadratic expansion forms a triangle table with the increase of N, called Yang Hui triangle. The matrix composed of the Yang Hui triangle table is called the Pascal matrix. The Pascal (n) function generates an n-order Pascal matrix. Example 22> X = Pascal (3) x = 1 1 1 1 2 3 1 69. Van Dimon Matrix

In Matlab, the function Vander (v) generates a model matrix based on the vector v.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.