Matlab programming and application series-Chapter 1 matrix operations (1)

Source: Internet
Author: User

This series of tutorials comes from the publication of the "Matlab-based programming basics and typical application books". If copyright concerns are involved, please contact [email protected]. Press: People's post and telecommunications press. The number of pages is 525.

This series of tutorials is currently based on MATLAB r2006a and may differ from functions in more advanced versions. If you have any questions in this tutorial, contact [email protected].

This chapter describes the content related to MATLAB matrix operations. Matrix operations are the foundation of MATLAB and the embodiment of powerful computing functions of MATLAB. The contents of this chapter can help readers solve some practical problems encountered in their learning and work, so that readers can understand basic matrix operation skills, and thus maximize the efficiency of MATLAB program execution. This chapter includes the following:

  • Basic matrix operations
  • Matrix decomposition
  • Solving Linear Equations
  • Sparse Matrix
3.1 basic matrix operations

The previous chapter has already introduced that there is no fundamental difference between two-dimensional arrays and matrices in appearance shape and data structure. However, as a mathematical transformation, matrix operations have strict mathematical rules. This section describes the basic operations of a matrix, including the four basic operations, multiplication operations, and operations of various matrix functions.

### Addition and subtraction operations
The matrix addition and subtraction operators are "+" and "-", respectively. Add and subtract corresponding elements, that is, according to the "+" and "-" operations of the matrix in linear algebra.

[Example 3.1] The known matrices A and B calculate the A-B and A + B respectively.
In the command window, enter the following two statements to generate the matrix A and matrix B.
& Gt; A = [1 2 3 4; 5 6 3 7; 3 3 2 1]; % use the direct matrix generation method to generate matrix A and matrix B
>> B=[3 4 5 6; 6 2 1 4; 9 6 5 3];
① Calculate the matrix A-B, enter the following content in the Command window:
& Gt; A-B % solution matrix Subtraction
The calculation result is as follows:
ans =<br/>-2 -2 -2 -2<br/>-1 4 2 3<br/>-6 -3 -3 -2
② Evaluate matrix A + B and enter the following content in the Command window:
& Gt; A + B % matrix addition
The calculation result is as follows:
ans =<br/>4 6 8 10<br/>11 8 4 11<br/>12 9 7 4

Note:(1) matrix A and matrix B must be of the same size before addition and subtraction can be performed.
(2) If either A or B is a scalar, the scalar is computed with each element of the matrix.

### Multiplication
The matrix multiplication operator is "*", which is performed by matrix multiplication in linear algebra, that is to say, the elements of each row in the preceding matrix are multiplied by the elements of each column in the following matrix and then added together.

(1) multiply two matrices

[Example 3.2] The matrix C and D are known, and z = C is calculated.D.
In the command window, enter the following two statements to generate the matrix C and matrix D.
& Gt; C = [2 4 6 8; 3 4 5 6; 9 6 3 1]; % generate matrix C and matrix D
&gt;&gt; D=[1 2 3;4 5 6;7 8 9;3 6 9];
Evaluate z = C
D. Enter the following content in the Command window:
& Gt; Z = C * D % solves the product of the matrix C and D
The result of multiplication of matrix C and matrix D is as follows:
Z =<br/>84 120 156<br/>72 102 132<br/>57 78 99

(2) multiplication of Matrices
The multiplication of a scalar and a matrix is the multiplication of a scalar and each element in the matrix.

For example, in [Example 3.2], find a = 2.C. Enter the following content in the Command window:
'> A = 2
C<Br/> the calculation result is as follows: <br/>A =
4 8 12 16
6 8 10 12
18 12 6 2'

Note: (1) when the matrix is multiplied, the number of columns in matrix A must be equal to the number of rows in matrix B unless one of them is a scalar.
(2) The Matrix Multiplication operation is different from the array Multiplication operation. The array multiplication operator is ". *", indicating that the corresponding elements in array A and array B are multiplied. Arrays A and B must be of the same size unless one of them is a scalar.

### Division
In Matlab, matrix Division has two operators "\" and "/", indicating the left division and right division of Matrix Operations respectively. If A or B is a non-singular matrix, a \ B = A <sup>-1 </sup>B or A/B =B <sup>-1 </sup>. Here, a <sup>-1 </sup> is the inverse of matrix. MATLAB provides the INV function used to calculate the inverse matrix of a matrix. Therefore, inv (a) can be used to obtain the inverse matrix of matrix. In general, x = A \ B is the equation.X = B, while X = B/A is the equation xThe result of the two operations is not equal.

Similar to the division operation of a matrix, arrays also have Division operators. The ". \" and "./" operators represent the left and right division of Array Operations, respectively, indicating that the corresponding elements of the array are mutually exclusive. During array division, the two arrays must have the same size unless one of them is a scalar.

[Example 3.3] The known matrices X1 and X2 are calculated as x1/X2 and X1 \ X2 respectively.
First, enter the following two statements in the Command window to generate the X1 and X2 matrices.
&gt;&gt; X1=[1 2 3;4 5 6;7 8 9];
& Gt; x2 = eye (3); % generates a diagonal matrix
① Calculate x1/X2 and enter the following in the Command window:
& Gt; x1/X2 % right division of the solution matrix
The calculation result of matrix x1/X2 is as follows:
ans =<br/>1 2 3<br/>4 5 6<br/>7 8 9
② Calculate X1 \ X2 and enter the following in the Command window:
& Gt; X1 \ X2 % left division of the solution matrix
The calculation result of matrix X1 \ X2 is as follows:
Warning: Matrix is close to singular or badly scaled.<br/>Results may be inaccurate. RCOND = 1.541976e-018.
ans =<br/>1.0e+016 *<br/>-0.4504 0.9007 -0.4504<br/>0.9007 -1.8014 0.9007<br/>-0.4504 0.9007 -0.4504

[Example 3.4] known equations $ \ begin {cases}
2x_1-x_2 + 3x_3 = 5 \
3x_1 + x_2-5x_3 = 5 \
4x_1-x_2 + X_3 = 9 \
\ End {cases}
$. Use matrix division to solve linear equations.

Convert the equation to the form of Ax = B, where,
$ \ BF {A }=\ begin {bmatrix} {2} & {-1} & {3} \ {3} & {1} & {-5} \ {4} & {-1} & {1} \ end {bmatrix} $, $ \ BF {B }=\begin {bmatrix} {5 }\{ 5 }\{ 9} \ end {bmatrix} $. The solution process is as follows.

First, enter matrix A and matrix B in the Command window.
&gt;&gt; A=[2 -1 3; 3 1 -5;<br/>4 -1 1];
& Gt; B = [5; 5; 9]; % input matrix A and B

Then, use matrix division to calculate the solution of the equations. In the command window, enter the following content:
& Gt; X = A \ B % use matrix division to solve equations
The result is as follows:
X=<br/>2<br/>-1<br/>0

In Linear Equations $In x = B $, the number of rows in level m x n matrix A represents the number of equations, and the number of columns n represents the number of unknowns. If n = m, $ A $ is a square matrix, 'a \ B = inv ()B; If M & gt; n is the least squares solution,X = inv ('A)('B); If M & lt; N, it is a special solution that makes N-M elements in X zero,X = inv ('A)('B )'.

### 3.1.4 Multiplication
The multiplication operator of the matrix is "^", which specifies that only the square matrix can perform multiplication. It can be divided into the following situations.
(1) When a is a square matrix and P is an integer greater than 0, $ A ^ p $ indicates the power P of A, that is, a multiplied by P; when P is an integer less than 0, $ A ^ p $ indicates the power P of the A-1. P cannot be computed when it is a matrix. An error occurs.
(2) When a is a square matrix and P is not an integer, $ A ^ P = V \ begin {bmatrix} {d{11} ^ p} & {}& {}\{}& {\ ddots} & {}\{}& {} & {d{NN} ^ p} \ end {bmatrix} V ^ {-1} $, where $ V $ is a feature vector of $ A $, $ \ begin {bmatrix} {d{11 }}<}\{}\{}&{\ ddots }&{}\{}&{}& {d{NN }}\\ end {bmatrix} $ indicates the diagonal matrix of the feature value. If there is a duplicate root, the preceding command is not valid.
(3) scalar matrix multiplication P ^ A. scalar matrix multiplication is defined as $ P ^ A = V \ begin {bmatrix} {P ^ {d{11 }}& {}& {}\{}& {\ ddots} & {}\{}& {}& {P ^ {d{NN }}\\ end {bmatrix} V ^ {-1 }$, in the formula V, D is taken from the feature value decomposition AV = AD.
(4) multiplication of scalar arrays P. ^ A. The multiplication of scalar arrays is defined as $ P. ^ A = \ begin {bmatrix} {P ^ {{11 }}& {\ cdots} & {P ^ {{1N }}\{\ vdots }&{\ ddots }&{\ vdots }\{ P ^ {{M1 }}& {\ cdots} & {P ^ {{Mn }}\\ end {bmatrix }$, an array multiplier A. ^ P, which represents the p-th multiplication of each element of.

[Example 3.5] The known matrix $ X_1 $ calculates the values of the matrix multiplication operator $ X_1 ^ 2 $, $ X_1 ^ {-1 }$, and $2 ^ {X_1} $ respectively.
In the command input window, enter the following matrix X1.
&gt;&gt; x1=[1 2 3;4 5 6;7 8 9];
Perform multiplication operations on the matrix based on Matrix X1, as shown in the following code:
① Calculate X1 ^ 2 and enter the following content in the Command window:
&gt;&gt; x1^2
The calculation result of X1 ^ 2 is as follows:
ans =<br/>30 36 42<br/>66 81 96<br/>102 126 150
② Calculate X1 ^-1 and enter the following content in the Command window:
&gt;&gt; x1^-1
The calculation result of X1 ^-1 is as follows:
Warning: Matrix is close to singular or badly scaled.<br/>Results may be inaccurate. RCOND = 1.541976e-018.
ans =<br/>1.0e+016 *<br/>-0.4504 0.9007 -0.4504<br/>0.9007 -1.8014 0.9007<br/>-0.4504 0.9007 -0.4504
③ Calculate 2 ^ X1 and enter the following content in the Command window:
&gt;&gt; 2^x1
The calculation result of 2 ^ X1 is as follows:
ans =<br/>1.0e+004 *<br/>0.7962 0.9782 1.1603 1.8029 2.2154 2.6276<br/>2.8097 3.4523 4.0950

Perform array multiplication based on the known conditions in example 3.5, and calculate 2. ^ X1 and x1. ^ X2 respectively.
In the command input window, enter the matrix X2:
&gt;&gt; x2=eye(3);
① Calculate 2. ^ X1. In the command window, enter:
&gt;&gt; 2.^x1
The result is as follows:
ans =<br/>2 4 8<br/>16 32 64<br/>128 256 512
② Calculate the Multiplication Side x1. ^ X2 of the array. In the command window, enter:
&gt;&gt; x1.^x2
The multiplication result is as follows:
ans =<br/>1 1 1<br/>1 5 1<br/>1 1 9

DT data
Contact information: [email protected]

Matlab programming and application series-Chapter 1 matrix operations (1)

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.