I. Input matrix
The best way to start learning Matlab is to know how to input a matrix.
You can use the following methods to input a matrix:
1. explicit list of direct input matrices
2. Import a matrix from an external data file
3. Generate a matrix using methods
4. Use the self-written method in the M file to generate a Matrix
Input matrix, you must follow the following rules:
1. Separate data in the same row with spaces or commas
2. Use a semicolon to mark the final data of each row
3. Use [] to enclose the entire data
For example
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
MATLAB will immediately display the matrix you just entered as follows:
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
This is actually a cube.
Ii. Sum, transpose matrix, and diagonal matrix
You may realize the characteristics of this cube, that is, if you sum up any row or column or any diagonal data, the results will be the same. Let's use MATLAB to represent these. The first expression you use is
Sum ()
MATLAB response:
Ans =
34 34 34 34
When no output variable is specified, Matlab uses the ANS (answer abbreviation) variable to represent the calculation result. You calculated the values of each column vector to 34.
What about the value of the row vector?
MATLAB gives priority to column vectors. The simplest way to obtain the value of a row vector is to transpose the original matrix, calculate the value of the row vector, and then transpose the result. We usually use a ellipsis or a quote to perform the transpose operation.
Therefore:
A'
Will generate:
Ans =
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 1
At the same time
Sum (')'
A column vector is generated to represent the value of each row vector.
Ans =
34
34
34
34
The data and values of the primary diagonal are mainly implemented using the sum and diag methods.
Diag ()
Generation:
Ans =
16
10
7
1
At the same time
Sum (DIAG ())
Generation:
Ans = 34
Another pair of angle data is also called the antidiagonal line. Because it is not important in mathematics, Matlab does not provide relevant implementation methods. However, in graphic design, fliplr can reverse a matrix from the left to the right.
Therefore:
Sum (DIAG (fliplr ()))
Will generate:
Ans = 34
I learned from this chapter:
I. Matrix generation
Ii. sum (A) of the matrix Summation method)
3. Matrix transpose:'
4. view the diag (a) element of the matrix)
5. view the anti-angle element fliplr (A) of the matrix)