matlab said simple matrix operation

Original address: represents a simple operation and a matrix matlab

First, the matrix representation
in MATLAB creation matrix has the following rules:

A, matrix elements must be in the "[]" inside;

B, between peer elements of the matrix with a space (or "") are separated;

row C, the matrix apart (or carriage return); and between the rows with "";

element d, the matrix can be a value, a variable, expression or function;

dimension E, the matrix need not be defined in advance.

Second, create a matrix:

1, the direct input

of the most simple way to create a matrix is input directly from the keyboard matrix elements, input methods according to the rules above. The vector can be established when the expression using the colon, the colon can produce a row vector expression, the general format is: e1: e2: e3, wherein an initial value e1, e2 in steps, e3 terminate value. Linspace generating function may also be row vectors, which call the format: linspace (a, b, n ), where a and b are the first and last elements to generate vector, n is the total number of elements.

 

2, create a matrix using MATLAB function

substantially matrix functions as follows:

(1) ones () function: to produce a full matrix 1, ones (n): generating n * n-dimensional matrix with all entries, ones (m, n): generating dimension m * n matrix of all 1's;

(2) zeros () function: to produce a full matrix 0;

(. 3) RAND () function: generating a random array (0,1) uniformly distributed;

(. 4) Eye () function: generating unit matrix;

(. 5) randn () function: mean of 0 and variance for a random matrix of standard normal distribution.

3, using the file creation matrix

when the matrix size larger or frequently used as a data matrix, you can save this file as a matrix, using the load command files directly transferred to the working environment can be used when needed. And can be transferred to reshape rearranged matrix with the command. reshape (A, m, n) , in which total elemental matrix remained constant, the matrix A re-arranged two-dimensional matrix of m * n.

Second, the simple matrix operation
1. Obtaining the matrix elements

can be referenced by the elements of the matrix index (row index), such as Matrix (m, n).

Reference may also be used to refer to the matrix element of matrix elements.

Is the number of matrix elements corresponding to elements in the order in memory.

In MATLAB, the matrix elements stored in columns.

Number (Index) and index (Subscript) is one to one, to m * n matrix A as an example, the matrix element A (i, j) as the number (j-1) * m + i.

Conversion relation which may be utilized sub2ind and ind2sub function determined.

2. Split matrix

using the colon expression submatrix obtained:

(. 1) A (:, j) represents the j-th column of the A matrix of all the elements taken; A (i, :) matrix represents all of the elements A i-th row; A (i , j) represents the i-th row taking the matrix a, column j element.

(2) A (i: i + m, :) represents taking all the elements of the i ~ + i m rows of the matrix A; A (:, k: k + m) represents taking a first matrix A k ~ k + m columns All the elements, a (i: i + m , k: k + m) represents the matrix of taking a i ~ i + m row, and all elements of k ~ k + m columns. In addition, vectors and in general may also be utilized to indicate end operator matrix index, so as to obtain sub-matrix. end superscript represents one dimension of the end elements.

Deleted using the empty matrix elements of the matrix:

in MATLAB, the definition of [] is empty matrix. Assigned to the variable X empty matrix statement X = []. Note that, X = [] different from X and Clear, Clear is deleted from the work space X, the empty matrix is present in the work space, but the dimension is 0.

3, a special matrix

(1) Rubik cube matrix matrix has an interesting property that each row, column, and two elements on the diagonal is the same. For n-order magic square, which element consists of l, 2,3, ..., a total of n2 and n2 integers. Providing Magic MATLAB function (n) required cube matrix, whose function is to generate a magic square of order n.

(2) Vandermonde matrix Vandermonde (of Vandermonde) last column matrix are all 1, the penultimate designated as a vector, each of the other rows and columns are thereafter dot product of the second column of the inverse. A vector generation can be used to specify a Vandermonde matrix. In MATLAB function Vander (V) generates basis vectors of the vector V is Vandermonde matrix.

Function (3) Hilbert matrix in MATLAB, generate Hilbert matrix is hilb (n). Inverse because small perturbations generated raw data unreliable results using general method. MATLAB, there is a special request function invhilb Inverse Hilbert matrix of (n), which is an inverse function matrix of order n Hilbert matrix.

(4) Toeplitz Toeplitz matrix (of Toeplitz) matrix except the first column of the first row, the other elements are the same as each element in the upper left corner. Generating a Toeplitz matrix function is toeplitz (x, y), which generates a first column at x, y is the Toeplitz matrix of the first row. Where x, y are vectors, both do not have equal length. toeplitz (x) x generates a symmetric Toeplitz matrix by a vector.

(5) generating function MATLAB adjoint matrix is accompanied Compan (p), where p is a polynomial coefficient vector, a high power factor in the top, in the bottom of low power.

(6) we know Pascal matrix, with a triangle composed of n increases quadratic coefficient table (x + y) n expanded referred Triangle. Triangle matrix of tables referred Pascal (Pascal) matrix. Pascal function (n) to generate a matrix of order n Pascals.

Third, the matrix operation

1, arithmetic

basic arithmetic MATLAB are: + (plus), - (minus), * (multiplication), / (right division), \ (left out), ^ (power), ' (Transpose). Operation is performed in the sense matrix arithmetic operation is just a special case of a single data.

(1) a matrix subtraction suppose two matrices A and B, can be achieved by adding and subtracting the matrix A + B, and AB. Operation rule is: If the same dimension of the matrix A and B, the respective elements may be performed subtraction matrix A and matrix B of addition and subtraction. If the number of dimensions A and B are not the same, then an error message will be given of MATLAB, prompts the user two matrix dimensions do not match.

(2) Assuming the matrix multiplication of two matrices A and B, if A is m * n matrix, B is n * p matrix, then C = A * B is a m * p matrix.

(3) in division MATLAB matrix, there are two division matrix: \ and /, respectively, in addition to left and right division. If the matrix A is nonsingular matrix, then A \ B and B / A calculation can be realized. A \ B is equivalent to A left inverse of matrix multiplication B, which is inv (A) * B, and the right against B / A is equivalent to the A matrix multiplication matrix B, that is, B * inv (A). For scalar arithmetic containing the same division of the two results. For the matrix, the left and right in addition to other represent two different relationship between the divisor and the dividend matrix matrix, generally A \ B ≠ B / A.

(4) the power of a power operation matrix may be represented as a matrix A ^ x, A is a square matrix claim, x is a scalar.

(5) a transposed matrix of real number matrix interchanging rows and columns, for complex matrices, conjugate transposition, special, operators' conjugate not transpose (see point arithmetic);.

(6) point operations in MATLAB , there is a special operation, as it is preceded by the operator about the point arithmetic operators, so called point of operation. Operators are point. *,. / ,. \ and ^. Two dot matrix computation elements refers to their corresponding correlation operation, it required two-dimensional matrix of the same parameters.

2, the relationship between arithmetic

MATLAB provides six relational operators: <(less than), <= (less than or equal),> (greater than),> = (greater than or equal to), == (equal to), ~ = (not equal to ). Relational operators algorithm is:

(1) When comparing two scalar amount, the size of the direct comparison of two numbers. If the relationship is established, the expression of a result relation, otherwise 0;

(2) when the amount involved in the comparison is the same when the two dimensions of the matrix, is a comparison of two matrix elements of the same position one by one according to the relationship between a scalar arithmetic rule, and gives the result of the comparison element. The end result of a relational operation is a dimension of the original matrix of the same matrix, its elements by a 0 or 1 composition;

(3) when participating in the comparison of a scalar, and the other is a matrix, put scalars and matrices each element of a scalar individually compared by calculating the relationship rules, and gives the result of the comparison element. The final result of a relational operation is the same dimension and the original matrix of the matrix, its elements composed of 0 or 1.

3, the logic

MATLAB provides three logical operators: & (and), | (or) - and (non). Algorithm logic operation:

(1) in the logic operation, true confirmed nonzero elements, denoted by 1, false zero element, represented by 0;

(2) provided two participating logic operation is a scalar and b then, a & b a, b whole is non-zero, the operation result is 1, and 0 otherwise. a | ba, b as long as there is a zero, as a result of the operation. ~ a when a is zero, the operation result is 1; and when a non-zero, the operation result is 0.

(3) if the logic operation is involved in the same two-dimensional matrix, the elements of the same position on the matrix calculation will be one at scalar rules. The final operation result is the original matrix with the dimension of the matrix, whose elements a 1 or 0 composition;

(4) If the participation of logical operations a scalar, a is a matrix, then the operation of each element of the scalar and matrix inter press scalar rules one by one. The final operation result is a matrix with dimension matrix whose elements a 1 or 0 composition;

(5) the logic NOT is unary operators, also subject matrix arithmetic rule;

(6) in the arithmetic relationship, logical operation, arithmetic operation with the highest priority, logical operation lowest priority.

Fourth, the matrix analysis

1, the diagonal matrix

(1) Only non-zero matrix is called a diagonal matrix elements on the diagonal of a diagonal matrix, the diagonal elements of the diagonal matrix known as an equal number of matrices, diagonal 1 line elements are the diagonal matrix called a unit matrix.

(1) extracting the matrix A is a diagonal elements provided m * n matrix, diag (A) function is used to extract the main diagonal elements of matrix A, to generate a column vector min (m, n) having elements. diag (A) function has the form diag (A, k), which is a function of the k-th diagonal element extraction.

(2) configured to set V diagonal matrix having m elements of the vector, diag (V) to generate a m * m diagonal matrix whose main diagonal element is the element of vector V. diag (V) function also has another form diag (V, k), whose function is to produce an n * n (n = m + k) diagonal, which is the m-th diagonal element is the vector V element.

2, triangular matrix

triangular matrix is further divided into upper triangular matrix and a lower triangular matrix, the so-called upper triangular matrix, i.e. the matrix below the diagonal elements of the matrix as a whole is 0, and the lower triangular matrix is above the diagonal the elements are all 0 as a matrix.

(1) A matrix is an upper triangular matrix MATLAB function is upper triangular matrix triu (A). triu (A) function also has another form triu (A, k), whose function is to find the elements above the diagonal matrix A k article.

Function in MATLAB, extracting a lower triangular matrix A (2) of the triangular matrix is TRIL (A) and tril (A, k), which use the extracted upper triangular matrix function triu (A) and triu (A, k) are identical.

3, the rotation matrix transpose of

(1) transposed matrix transpose operator is a single apostrophe ( ').

The rotation function by using rot90 (A, k) (2 ) A rotation matrix is a matrix of k times 90o, when k is 1 can be omitted.

4, a matrix inversion

of a matrix inversion is the original left and right embodiments the first column and the last column of the matrix exchange, the second exchange column and the penultimate column, ..., and so on. A matrix inversion function is approximately embodiment fliplr (A), the function of the matrix A is turned upside embodiment flipud (A).

5, the pseudo-inverse and inverse matrix

(1) for an inverse matrix of a matrix A, if there is a same order as their matrix B, such that: AB = BA = I (I is a unit matrix) is called A, B inverse matrix, of course, a is the inverse matrix B. Find the inverse matrix A square callable function inv (A).

(2) if the pseudo-inverse matrix of matrix A is not a square matrix or square matrix A is a non-full rank of the matrix A is no inverse matrix, but can be found a 'matrix B isotype of the transposed matrix A and A, and such that: ABA = a, BAB = B B at this time is called pseudo-inverse matrix a is also known as generalized inverse matrix. In MATLAB, seeking a pseudo inverse matrix function is pinv (A).

6, the determinant square

to a square as a determinant, and evaluated according to the rules of its determinants, this value is referred to the corresponding value matrix determinant. In MATLAB, the function value corresponding to a square matrix A is the determinant det (A).

7, with the rank of the matrix tracks

(1) the number of rows of linear matrix rank matrices independent of the rank of the matrix referred to the number of columns. In MATLAB, the matrix function evaluation rank is the rank (A).

Trace of a matrix (2) is equal to trace the matrix elements of the matrix diagonal and also equal to the sum of eigenvalues. In MATLAB function of Matrix is trace trace (A).

8, the vector and matrix norm

norm vector or matrix used to measure the length of a vector or matrix in some sense. There are many ways defined norm, which defines different, norm value is different.

Function (1) three kinds of commonly used vector norm calculation and function in MATLAB, find the vector norm is:

2-norm of the vector V is calculated;: a, norm (V) or NORM (V, 2)

B , norm (V, 1): 1- norm computation vector V;

C, nORM (V, INF): calculated number ∞- vector V norm.

Norm calculation function and MATLAB (2) provides the matrix function evaluation of three kinds of matrix norm, which is a function of the function call format vector norm requirements exactly.

Number Condition (3) in the MATLAB matrix, three kinds of function of the condition number is calculated matrix A:

the number of 1-norm conditions (A,. 1) A calculation of a, cond;

Under the condition number calculated A number of 2-norm of b, cond (A) or cond (A, 2);

condition number under c, the number ∞- norm cond (A, inf) A is calculated.

9, eigenvalue and eigenvector matrix

in MATLAB calculation function eigenvalues and eigenvectors of the matrix A is EIG (A), commonly call format There are three:

(. 1) E = EIG (A): Solving Matrix a characteristic of all values of vector composition E.

(2) [V, D] = eig (A): find all the eigenvalues of the matrix A, constitute a diagonal matrix D, and determining the eigenvectors of A column vector V.

(3) [V, D] = eig (A, 'nobalance'): similar to the second type format, but after the two kinds of format prior to A as a similarity transformation of Matrix A eigenvalues and eigenvectors, and format 3 matrix a directly find the eigenvalues and eigenvectors.

Five string

in MATLAB, the string is a sequence of characters enclosed in single prime number. MATLAB string as a row vector, each element corresponding to a character that identifies the same and numerical vector. You can also create multi-line strings matrix. String is stored in the form of ASCII code. abs double function and can be used to obtain a matrix of values in ASCII string corresponding to the matrix. Instead, char ASCII code matrix function can be converted to a string matrix. Another important function is associated with the string eval, which call the format: eval_r (t) where t is a string. Its role is as the contents of the string corresponding to execute MATLAB statements.

Six other

to see the distribution of non-zero element matrix spy (A);

applying a second partial matrix of

a sparse matrix

For a matrix of order n, n2 typically requires storage space, when n is large, it will take a lot of computation time and memory space matrix operation. Large Scale Matrix encountered in many practical problems usually contains a lot of elements 0, this matrix is called a sparse matrix. Matlab supports sparse matrices, storing only the non-zero elements of the matrix. Since that is not stored, "0" elements, they are not operated, thereby saving calculation time and memory space, which calculates the cost and complexity depends only on the number of non-zero elements of the sparse matrix, which matrix in the storage space and have a great advantage on the computation time.

Is defined as the number density of the matrix is non-zero elements in the matrix divided by the total number of elements in the matrix. For the low density matrix is sparse stored a good choice.

1, create a sparse matrix

(1) The conversion was complete storage sparse storage function A = sparse (S) matrix S into a sparse matrix A. The storage When the storage matrix S are sparse, then the function call corresponds to A = S. There are other sparse function call format: sparse (m, n): generating an m * n sparse matrix all elements are 0's. sparse (u, v, S) -: u, v, S is a vector of length 3 and so on. S is a sparse matrix to establish a non-zero elements, u (i), v ( i) are the S (i) of the row and column index, the function creates a max (u) line, max (v) column and where S is the sparse sparse matrix elements. In addition, there are some sparse matrix operations and related functions. full (A): A matrix corresponding to the return is completely and sparse matrix storage memory.

(2) directly create a sparse matrix S = sparse (i, j, s, m, n), where i and j are row and column matrix of non-zero elements indicator vector, s vector value is non-zero elements, m, n, respectively, is the number of rows and columns of a matrix.

(3) create a sparse matrix using a function of load and can be entered spconvert sparse matrix from a text file contains a list of the index and the non-zero elements from the file. Example: Let T.txt text file has three columns, the first column is the row index number, the second column is a column index, the third column is the non-zero element values. load T.txt S = spconvert (T) .

Create S (. 4) is sparse banded matrix = spdiags (B, d, m , n) wherein m and n are numbers of rows and columns of the matrix; D is the length of the vector p is an integer that specifies the matrix S Jiaoxian position; B is a matrix all elements, the elements of S for a given diagonal positions, the number of rows is min (m, n), is the number of columns p.

(5) Other functions to create a sparse matrix

S = Speye (m, n-)

S = Speye (size (A)) The% Same size has AS A

S% = Buchy a built-sparse matrix (the adjacency matrix)

, etc.

2, sparse operational matrix

sparse matrix storage just different storage array, its operational rules and common matrix is the same, it can be directly involved in the operation. Therefore, Matlab the full matrix of operations and functions are also available in the sparse matrix. The result is a sparse matrix or full matrix, depending on the operator or function. When not all objects involved in computing the memory matrix is sparse, the results are generally stored completely in the form.

3. Other

(1) nonzero element information

nnz (S)% Returns the number of non-zero elements

nonzeros (S)% Returns the column vector contains all non-zero elements

nzmax (S)% of the total return is assigned to a sparse matrix in the non-zero entries storage space

(2) View sparse matrix shape Spy (S)

(. 3) Find function sparse matrix

[I, J, S] = Find (S)

[I, J] = Find (S)

returns all non-S, and the subscript value of zero elements, S may be a sparse matrix or full matrix.


Address reprint this article please specify: representation matlab matrix and simple operation

Guess you like

Origin www.cnblogs.com/fanglijiao/p/11205808.html