# MATLAB matrix [Z]

Source: Internet
Author: User

Part 1: Basic matrix knowledge
I. Create a Matrix

Direct Input Method
Use Matlab functions to create a Matrix
Create a matrix using a file
Ii. Matrix splitting

Matrix Element
Matrix splitting
Special Matrix
Iii. Matrix Operations

Arithmetic Operations
Relational operation
Logical operation
Iv. Matrix Analysis

Diagonal Array
Triangle Array
Transpose and rotating a Matrix
Matrix flip
Matrix Inverse and pseudo inverse
The determining factor of the Square Matrix
Rank and trace of a Matrix
Vector and matrix norm
Matrix feature value and feature vector
5. String
Vi. Others

Part 2 Application of Matrix
I. Sparse Matrix

Create a sparse matrix
Sparse Matrix Operations
Others
Ii. matrices in Finite Fields

Content

Part 1: Basic matrix knowledge (only for basic introduction. For details, refer to the Matlab help Documentation)

A matrix is a basic element for data processing and computation. In MATLAB
A. Generally, the number (scalar) can be regarded as a matrix of "1*1;
B. the n-dimensional vector can be regarded as the matrix of "n * 1;
C. The polynomial can be completely determined by its coefficient matrix.

I. Create a Matrix

Creating a matrix in MATLAB has the following rules:
A. The matrix element must be in;
B. the peat elements of the matrix are separated by spaces (or;
C. Separate rows and rows in the matrix with ";" (or carriage return;
D. The matrix element can be a value, variable, expression, or function;
E. The matrix size does not need to be pre-defined.

The following describes how to create four matrices:

1. Direct Input Method
The simplest method to create a matrix is to directly input the elements of the matrix from the keyboard. The input method follows the above rules. A colon expression can be used to create a vector. A colon expression can generate a row vector in the following format: E1: E2: E3. E1 is the initial value, E2 is the step size, and E3 is the end value. You can also use the linspace function to generate a row vector. The call format is linspace (A, B, n), where A and B are the first and last elements of the generated vector, n is the total number of elements. It can be seen that linspace (A, B, n) is equivalent to a :( B-A)/(n-1): B.

2. Use MATLAB functions to create a Matrix
The basic matrix functions are as follows:
(1) ones () function: generate a matrix of all 1, ones (n): Generate N * n-dimensional full 1 matrix, ones (m, n ): generates a m * n-dimensional full 1 matrix;
(2) zeros () function: generate a matrix of all 0;
(3) rand () function: generates a random array with an even distribution in the (0, 1) interval;
(4) Eye () function: generates a unit array;
(5) randn () function: generate a Standard Normal Distribution Random matrix with the mean value 0 and variance 1.

3. Create a matrix using files
When the matrix size is large or frequently used data matrix, you can save the matrix as a file and directly use the load command to transfer the file to the work environment as needed. At the same time, you can use the command reshape to rearrange the imported matrix. Reshape (A, m, n), which reassembles matrix A into A two-dimensional m * n matrix while the total elements of the matrix remain unchanged.

Ii. Matrix splitting

1. Matrix Elements
You can use subscript (row-column index) to reference Matrix elements, such as Matrix (m, n ). You can also use the serial number of the matrix element to reference the matrix element. The sequence number of the matrix elements is the order of the corresponding elements in the memory. In MATLAB, matrix elements are stored by column, first the first column, then the second column, and so on. Index and Subscript are one-to-one correspondence. Taking m * n matrix A as an example, the sequence number of matrix element A (I, j) is (J-1) * m + I. The conversion relationship can also be obtained using sub2ind and ind2sub functions.

2. Matrix splitting
Use a colon expression to obtain the child matrix:
(1) A (:, j) indicates all elements in column j of matrix A; A (I, :) indicates all elements in row I of matrix A; A (I, j) indicates the elements in row I and column j of matrix.
(2) A (I: I + m, :) indicates taking the I ~ of the Matrix ~ All elements of rows I + m; A (:, k: k + m) indicates the k ~ of the matrix ~ All elements in the k + m column. A (I: I + m, k: k + m) indicates that the I ~ of the matrix is obtained ~ In the I + m row, and in the k ~ All elements in the k + m column. In addition, the normal vector and end operator can be used to represent the matrix subscript to obtain the submatrix. End indicates the subscript of the end element of a dimension.
Delete a matrix element using an empty matrix:
In MATLAB, [] is defined as an empty matrix. The statement for assigning an empty matrix to variable X is X = []. Note that X = [] is different from clear X. clear deletes X from the workspace, while the empty Matrix exists in the workspace, except that the dimension is 0.

3. Special Matrix
(1) Cube matrix cube matrix has an interesting property. Each row, column, and two diagonal lines have the same elements. For Rank-n cube arrays, the elements include: 1, 2, 3 ,..., N2 is composed of n2 integers. MATLAB provides the magic (n) function for finding the cube matrix. Its function is to generate an n-level cube array.
(2) The last column of the van dermon matrix is 1, and the last column is a specified vector. The other columns are the product of the vertex between the last column and the last column. You can use a specified vector to generate a model matrix. In MATLAB, the function vander (V) generates a model matrix based on the vector V.
(3) In MATLAB, the function for generating the Hilbert matrix is hilb (n ). Using the general method to calculate the inverse will produce unreliable computation results due to small disturbance of the original data. In MATLAB, there is an invhilb (n) function dedicated to finding the inverse of the Hilbert matrix of n order.
(4) The top litz matrix top litz (Toeplitz) except for the first column of the First line, each element is the same as the element in the upper left corner. The toeplitz (x, y) function is used to generate the top litz matrix with x as the first column and y as the first row. Here, x and y are vectors, and they do not have to be equal to the length. Toeplitz (x) uses vector x to generate a symmetric Toplitz matrix.
(5) The Adjoint Matrix Function generated by MATLAB is compan (p). p is the coefficient vector of a polynomial, and the higher power coefficient is placed before and the lower power is placed behind.
(6) Pascal matrix we know that the coefficient after the expanded quadratic (x + y) n forms a triangle table with the increase of n, which is called the 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.

Iii. Matrix Operations

1. Arithmetic Operations
The basic arithmetic operations of MATLAB include: + (plus),-(minus), * (multiplication),/(right division), \ (left Division), and ^ (multiplication), '(transpose ). Operations are performed in a matrix. arithmetic operations on a single data are only a special case.
(1) matrix addition and subtraction operations assume that there are two matrices A and B, then A + B and A-B can realize the addition and subtraction operations of the matrix. The calculation rule is: if the dimension of matrix A and matrix B are the same, the addition and subtraction operations of the matrix can be performed, and the corresponding elements of matrix A and matrix B are added and subtracted. If the dimensions of A and B are different, MATLAB will give an error message indicating that the dimensions of the two matrices do not match.
(2) matrix multiplication assumes that there are two matrices A and B. If A is m * n and B is n * p, C = A * B is m * p.
(3) matrix division in MATLAB, there are two types of matrix Division operations: \ and/, indicating the left division and Right Division respectively. If matrix A is A non-singular matrix, A \ B and B/A operations can be performed. A \ B is equivalent to A's inverse left multiplication matrix B, that is, inv (A) * B, while B/A is equivalent to A's inverse right multiplication matrix B, that is, B * inv (). For operations that contain scalar values, the results of the two Division operations are the same. For A matrix, the left division and the right division represent the relationship between the two different divisor matrices and the divisor matrices. Generally, A \ B = B/.
(4) multiplication of A matrix the multiplication operation of A matrix can be expressed as A ^ x, requiring A to be A square matrix and x to be A scalar.
(5) The transpose of the matrix swaps the rows and columns of the real number matrix. This applies to the complex matrix, and is a special operator. 'condensed is not transposed (see vertex operations );
(6) vertex operations in MATLAB have a special operation, because the operator is before the arithmetic operator, so it is called a vertex operation. Vertex operators include. *,./,. \, And. ^. Vertices of two matrices are related operations on their corresponding elements. The dimension parameters of the two matrices must be the same.

2. relational operations
MATLAB provides six Relational operators: <(less than), <= (less than or equal to),> (greater than),> = (greater than or equal to), and = (equal) ,~ = (Not equal ). The Relational operators are calculated as follows:
(1) When two comparisons are scalar values, the two numbers are directly compared. If the link is true, the result of the link expression is 1; otherwise, the result is 0;
(2) When the quantity involved in the comparison is two matrices with the same dimension, the comparison is to compare the elements at the same position of the two matrices one by one according to the calculation rules of the scalar relationship and give the element comparison result. The final relational calculation result is a matrix with the same dimension as the original matrix. Its elements are composed of 0 or 1;
(3) When one involved in the comparison is a scalar and the other is a matrix, the scalar and each element of the matrix are compared one by one according to the calculation rules of the scalar relationship, and the element comparison result is given. The final relational calculation result is a matrix with the same dimension as the original matrix. Its elements are composed of 0 or 1.

3. logical operations
MATLAB provides three logical operators: & (and), | (OR), and ~ (Not ). The logic operation rule is:
(1) In logical operations, it is confirmed that the non-zero element is true, expressed as 1, zero element is false, and 0 is used;
(2) If two scalar A and B are involved in the logical operation, the values of A & B a and B are all non-zero and the calculation result is 1. Otherwise, the value is 0. A | B a, B. If there is a non-zero value, the calculation result is 1 .~ When a is zero, the calculation result is 1. When a is not zero, the calculation result is 0.
(3) If two cognominal matrices are involved in the logical operation, the calculation will perform the elements at the same position of the matrix one by one according to the scalar rules. The final calculation result is a matrix with the same dimension as the original matrix. Its elements are composed of 1 or 0;
(4) If one involved in the logical operation is a scalar and the other is a matrix, the operation will be performed one by one between the scalar and each element in the matrix according to the scalar rules. The final calculation result is a matrix with the same dimension as the matrix. Its elements are composed of 1 or 0;
(5) The logic is not a single-object operator, but also follows the matrix operation rules;
(6) In arithmetic, relational, and logical operations, arithmetic operations have the highest priority and logical operations have the lowest priority.

Iv. Matrix Analysis

1. Diagonal Arrays
(1) only a matrix with non-zero elements on the diagonal line is called a diagonal matrix. a diagonal matrix with equal elements on the diagonal line is called a quantitative matrix, the diagonal matrix of elements on the diagonal line is called the unit matrix.
(1) set a as M * n as the diagonal element of the extraction matrix. The diag (a) function is used to extract the primary diagonal element of matrix A and generate) element column vector. The diag (a) function also has a form of diag (A, K), which is used to extract the elements of the nth diagonal line.
(2) construct a diagonal matrix. If V is a vector with m elements, diag (V) generates an m * m diagonal matrix. Its main diagonal element is the element of vector V. The diag (V) function also has another form of diag (V, k). Its function is to generate an n * n (n = m + k) diagonal array, the m-th diagonal line is the element of vector V.

2. Triangular Array
The triangular array is further divided into the upper and lower triangular arrays. The so-called upper triangular array is a matrix in which all the elements below the diagonal line of the matrix are 0, the lower triangle array is a matrix with all the elements above the diagonal line being 0.
(1) Calculate the upper Triangle Matrix of matrix A. The MATLAB function of the upper triangle matrix is triu (). Triu (A) function also has another form of triu (A, k). Its function is to find the elements above the diagonal line of the k th line of matrix.
(2) In MATLAB, the function of extracting the lower Triangle Matrix of matrix A is tril (A) and tril (A, k ), its usage is exactly the same as the triu (A) and triu (A, k) functions used to extract the upper triangle matrix.

3. Transpose and rotate a Matrix
(1) The transpose operator of the matrix is a single apostrophes (').
(2) the rotation of the matrix uses the rot90 (A, k) function to rotate matrix A k times of 90o. When k is 1, it can be omitted.

4. Matrix flip
The left-right flip of the matrix is to swap the first and last columns of the original matrix, and the second and last columns ,..., And so on. The fliplr (A) function is used to implement left-right flip of matrix A, and flipud (A) is used to flip up and down matrix ).

5. matrix inverse and pseudo inverse
(1) For A matrix A, if there is A matrix B of the same order, the AB = BA = I (I is the unit matrix) B is called the inverse matrix of A. Of course, A is also the inverse matrix of B. The inv (A) function can be used to calculate the inverse matrix of matrix ).
(2) If matrix A is not A matrix or A is A non-full-rank matrix, matrix A has no inverse matrix, however, we can find A matrix B that is the same as the transpose matrix A' of A, so that ABA = A, BAB = B is called matrix B as the pseudo inverse of matrix, it is also called generalized inverse matrix. In MATLAB, the function for finding A matrix pseudo-inverse is pinv ().

6. Determine the phalanx
Think of a square matrix as a deciding factor and evaluate it according to the rule of the determining factor. This value is called the value of the determining factor corresponding to the matrix. In MATLAB, det (A) is the function that calculates the value of the determinant corresponding to square matrix ).

7. Rank and trace of the matrix
(1) the number of rows and columns in the rank matrix of a matrix are called the rank of a matrix. In MATLAB, the rank function of the matrix is rank ().
(2) the trace of the matrix is equal to the sum of the diagonal elements of the matrix and the sum of the feature values of the matrix. In MATLAB, the trace function of the matrix is trace ().

8. Vector and matrix norm
The matrix or Vector norm is used to measure the length of a matrix or vector in a certain sense. There are multiple methods to define a norm. The norm value varies depending on its definition.
(1) The three common vectors and their calculation functions are in MATLAB. The functions for finding the vector norms are:
A, norm (V) or norm (V, 2): Calculate the 2-norm of vector V;
B. norm (V, 1): calculates the 1-norm of vector V;
C. norm (V, inf): Calculate the ∞-norm of vector V.
(2) matrix norm and its calculation function MATLAB provide functions for finding three matrix norm. The function call format is exactly the same as that for Vector norm functions.
(3) The condition number of the matrix is in MATLAB. The function for calculating the three conditions of matrix A is:
A. cond (A, 1) calculates the number of conditions under the 1-norm of;
B. cond (A) or cond (A, 2) Calculate the conditional number under the 2-norm number of;
C. cond (A, inf) calculates the number of conditions under the ∞-norm of.

9. Matrix feature values and feature vectors
In MATLAB, eig (A) is used to calculate the feature values of matrix A and feature vector functions. There are three commonly used call formats:
(1) E = eig (A): calculate all the feature values of matrix A, and construct the vector E.
(2) [V, D] = eig (A): evaluate all the feature values of matrix A, form the diagonal matrix D, and obtain the feature vectors of A to form the column vectors of V.
(3) [V, D] = eig (A, 'nobalance '): similar to the 2nd format, however, in the first 2nd formats, the feature values and feature vectors of matrix A are obtained after the similarity transformation of A, while in the third format, the feature values and feature vectors of matrix A are obtained directly.

5. String

In MATLAB, a string is a sequence of characters enclosed by a single marker. MATLAB treats a string as a row vector, and each element corresponds to a character. Its identification method is the same as that of a numeric vector. You can also create a multi-row string matrix. Strings are stored as ASCII codes. Both abs and double functions can be used to obtain the ASCII code numeric matrix corresponding to the string matrix. On the contrary, the char function can convert the ASCII matrix into a string matrix. Another important function related to the string is eval. Its call format is: eval_r (t) where t is the string. It is used to execute the string content as the corresponding MATLAB statement.

Vi. Others

View the distribution of non-zero elements in the matrix spy ();

Part 2 Application of Matrix

I. Sparse Matrix

For an n-order matrix, n2 storage space is usually required. When n is large, matrix operations occupy a large amount of memory space and computing time. In many practical problems, a large-scale matrix usually contains a large number of 0 elements. Such a matrix is called a sparse matrix. Matlab supports sparse matrices and only stores non-zero elements of the matrix. Because the "0" elements are not stored or operated on, the memory space and computing time are saved. the complexity and cost of computing only depend on the number of non-zero elements in the sparse matrix, this has great advantages in matrix storage space and computing time.
The density of a matrix is defined as the number of non-zero elements in the matrix divided by the total number of elements in the matrix. Sparse storage is a good choice for low-density matrices.

1. Create a sparse matrix
(1) convert the full storage mode to the sparse storage mode function a = sparse (s) and convert matrix s to the sparse storage mode matrix. When Matrix S is a sparse storage method, the function call is equivalent to a = S. The sparse function has other calling formats: sparse (m, n): generates a sparse matrix where all elements of M * n are 0. Sparse (u, v, s) --: U, V, S is three equi-length vectors. S is the non-0 element of the sparse matrix to be created. U (I) and V (I) are the rows and column subscript of S (I), respectively, this function creates a sparse matrix of max (u) rows and max (v) columns with S as sparse elements. In addition, there are some functions related to sparse matrix operations. Full (a): returns the full storage mode matrix corresponding to sparse storage matrix.
(2) directly create the sparse matrix S = sparse (I, j, S, m, n), where I and j are the rows and column indicator vectors of non-zero elements of the matrix, S is a non-zero element value vector, and m and n are the number of rows and columns of the matrix, respectively.
(3) create a sparse matrix from a file. Using the load and spconvert functions, You can input a sparse matrix from a text file containing a series of subscripts and non-zero elements. For example, if the text file t.txt contains three columns, the first column is the row subscript, the second column is the column subscript, and the third column is the non-zero element value. Load t.txt S = spconvert (t ).
(4) create a sparse band matrix S = spdiags (B, d, m, n) Where m and n are the number of rows and columns of the matrix respectively; D is an integer vector whose length is P. It specifies the diagonal position of matrix S. B is a full element matrix, which is used to specify the elements at the diagonal position of S. The number of rows is Min (m, n ), the number of columns is P.
(5) create functions for other sparse Matrices
S = speye (m, n)
S = speye (SIZE (A) % has the same size as
S = Buchy % a built-in sparse matrix (Adjacent matrix)
And so on.

2. sparse matrix operations

The sparse storage matrix is only stored in different ways. It has the same operation rules as normal matrices and can be directly involved in operations. Therefore, full matrix operations and functions in Matlab can also be used in sparse matrices. Whether the result is a sparse matrix or a full matrix depends on the operator or function. When the objects involved in the operation are not all in the sparse storage matrix, the results are generally fully stored.

3. Others

(1) Non-zero element Information
Nnz (S) % returns the number of non-zero elements
Nonzeros (S) % returns the column vector, containing all non-zero elements
Nzmax (S) % returns the total storage space allocated to non-zero items in the sparse matrix.
(2) view the shape of the sparse matrix spy (S)
(3) find function and sparse matrix
[I, j, s] = find (S)
[I, j] = find (S)
Returns the subscript and value of all non-zero elements in S. S can be a sparse matrix or a full matrix.

Ii. matrices in Finite Fields

Matrix Operations in channel coding are generally based on finite fields. Therefore, we need to convert normal Matrices Into matrices in finite fields so that they can be computed in GF (m. You can use the command gf (data, m) to limit the data to a finite field. In this way, operations such as matrix inversion, addition, and multiplication are all based on the GF (m) Operation of the finite field.

Note: if there is a problem with the matrix display written with LaTeX, the "&" symbol is displayed in the image in the html language. Which of the following can help solve this problem? Thank you!
Solution: Use \; instead &. However, it seems that the appearance is not good enough. It is estimated that it is a bug of the Latex Math plug-in. I don't know if there is any better solution.

Original: http://blog.sina.com.cn/s/blog_5d236e060100bv6v.html

Related Keywords:

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.