Matlab Chapter II
Vector
*
represents a vector multiplication;
'
denotes vector transpose;
MATLAB allows the vector and combined: w = [ u; v ]
or f = [ u v ]
;
we can use x = [0(初值):2(步长*可以为负):10(终止值)]
to create an even number of 0 to 10 o'clock vector selected from the group:
>> x = [ 0; 2; 10]
x =
0 2 4 6 8 10
.^
Represents the power of a vector, and can not directly use the ^ symbol
linspace(a,b,n)
to create a vector containing n arithmetic elements between a and b;
Vector operations
dot(a,b)
or .*
both can represent vector dot product;
we can use the following command to calculate the modulus of a vector:
>> J = [ 0; 3; 4];
mag = sqrt(dot(a,a))
mag =
5
cross(A,B)
Represents the cross product of a vector, the vector of the cross product must be three-dimensional:
>> A = [ 1 2 3]; B = [ 2 3 4];
>> C = cross(A, B)
C =
-1 2 -1
V(i)
Refers to the i-th element of v;
v(:)
will refer to all elements;
v(4:6)
indicates elements within a certain range, for example:
>> v = A (4:6)
v =
0
4
4
Indicates that the 4th to 6th elements from A are selected to form a new vector
Matrix
.*
represents the array multiplication of the matrix (not matrix multiplication), which means the corresponding elements are multiplied;
*
it represents the matrix multiplication, which requires the matrix to be operated to meet the conditions of matrix multiplication;
./
and .\
respectively represent the right and left division of the array;
eye(n)
you can create nxn unit matrix, zeros(n)
you can create nxn zero matrix, ones(n)
you can create nxn 1 matrix;
Quoting
a single element or an entire column of a matrix in Matlab can be quoted:
A = [ 1 2 3; 4 5 6; 7 8 9]
we can use `A(m,n) to select elements of m rows and n columns:
>> A(2,3)
ans =
6
You can use A(:,i) to refer to all elements in the i-th column:
>> A (:,2)
ans =
2
5
8
- You can use A(:,i:j) to select all elements from the i-th column to the j-th column;
- You can use A(m:n,i:j) or A([m,n],[i,j]) to select the submatrix;
You can delete rows or columns of a matrix by using an empty array:
>> A(2,:)=[]
A =
1 2 3
7 8 9
The above operation turns the 3x3 matrix into a 2x3 matrix;
Determinant and linear solution
det(A)
means calculating the determinant of matrix A:
>> A = [1 3; 4 5]
>> det(A) =
ans =
-7
Analyzing the case we can use the determinant of matrix solution, when we need to represent a plurality of solutions, we need to train a set of base solution:
null()
function represents the null space matrix, we can use null(A,'r')
to return a set of rational basis Solution:
>> A = [3 0 -1 0; 8 0 0 -2; 0 2 -2 -1];
z=null(A)
z =
700/4999
502/717
799/1902
601/1073
>>y=null(A,'r')
y =
1/4
5/4
3/4
1
- rank(A)=n is equivalent to an empty matrix with null(A) being nx0, that is, Ax=b has a unique solution;
Rank & inverse matrix
The rank of a matrix is a measure of the linear independence between matrix vectors, which can be rank(A)
calculated using;
We can also judge the solution situation by rank:
For mxn-order matrix Ax=b, the system has a solution if and only when rank(A)=rank(A b); if the rank is equal to n, the solution is unique; if the rank is less than n, there are infinitely many solutions;
inv(A)
Represents the inverse matrix of A. The inverse matrix exists if and only if det(A) is not equal to 0. We call it an invertible matrix or a non-singular matrix (such a matrix must be full rank);
- Matlab can also find the pseudo-inverse matrix (or generalized inverse matrix):
pinv(A)
The trapezoidal matrix
rref(A)
can find the simplest trapezoidal matrix of A. For example, for the magic square matrix, the hand is very complicated, and matlab can easily handle it:
>> A=magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> rref(A)
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
- magic(n) is the syntax for finding a magic square matrix of order n. In such a matrix, the sum of the rows and columns including the diagonals are equal. Let us verify
The syntax structure of sum: sum(A,dim)
A represents a matrix, dim={1,2}; 1 represents the sum of columns, 2 represents the sum of rows
Let's continue to take just A as an example:
>> sum(A,1)
ans =
65 65 65 65 65
>> sum(A,2)
ans =
65
65
65
65
65
Then sum the diagonals through a simple loop:
>> e=0;d=0;
for i=1:5; j=6-i;
b=A(i,i); c=A(i,j);
d=d+b; e=e+c;
end;
e,d
e =
65
d =
65
It is not difficult to see that the sum of rows, columns and diagonals are equal
Matrix decomposition
matlab can quickly decompose a matrix of various types:
[L,U]=lu(A)
Represents LU decomposition of A;
>> A= [-1 2 0; 4 1 8; 2 7 1];
[L,U]=lu(A)
L =
-1/4 9/26 1
1 0 0
1/2 1 0
U =
4 1 8
0 13/2 -3
0 0 79/26