Basics: data type
Infinity: Inf or inf
Pi: pi
String: name='jack'
String array: char('jack','lucy')
>> a=char('lucy','tony')
a =
2×4 char 数组
'lucy'
'tony'
Integer type: uint8, uint16, uint32, uint64, int8, int16, int32, int64
View the most value: intmin('int8'), intmax('int8')
>> intmin('int8')
ans =
int8
-128
Floating point number: double (default), single
Floating point precision (minimum resolution of numbers): eps('single'), eps('double')
Floating point precision (the maximum and minimum values of a number): realmax('single'), realmin('double')
>> eps('single')
ans =
single
1.1921e-07
realmax('single')
ans =
single
3.4028e+38
Basics: Variables
Operators: +, -, *, /, \, ^
Variable assignment: x=15, x=3*x-12
Variable operation: a=12, b=4, c=a+b-2/b
Variable printing control: use; control
Variable naming: beginning with a letter, up to 63 characters, can contain numbers, letters, and underscores, case-sensitive, no brackets, spaces, and built-in keywords (such as length, sum, end, pi, i, j (imaginary number), eps, sin, cos, size, etc.)
View the built-in variables (keywords): iskeyword
View variables in the workspace: whos
Basics: commonly used built-in functions
Square root: sqrt(x)
Open the nth power: nthroot(x,n)
Exponent: exp(x)
Absolute value: abs(b)
Logarithm: log(x), log10(x) (default is base e)
Trigonometric functions: sin(x), cos(x)...
Myopia function: Take the nearest integer, that is, round (x), round to zero fix(x), round up ceil(x), round down floor(x), and take the remainder rem(x)
Clear memory variables: clear xyz, clear all, clc (clear command window)
View memory variables: who, whos
Vector basis
Row vector: A=[1 2 3 4] or A=[1,2,3,4]
Column vector: A=[1;2;3;4]
Row vector to column vector: B=A(:) or B=A'
View size: size(A), length(A)
>> A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>> size(A)
ans =
3 2
>> length(A)
ans =
3
size represents the size of the viewing matrix: several rows and several columns
length view the size of the vector: a few lines
Slicing method:
Start from the second element, take it to the end, and number in the order of the column. End can also be replaced by -1, end-1 means to the second to last, end-2 means to the third to last
>> A(2:end)
ans =
3 5 2 4 6
Starting from the second element, taking 1 as the step size, get to the fourth element
A(2:4)
ans =
3 5 2
Starting from the second element, using 2 as the step size, take the fifth element
>> A(2:2:5)
ans =
3 2
Take discrete values:
>> A([1,3,4])
ans =
1 5 2
Create a linearly distributed vector:
A=1:2:100, starting from 1, take a number every 3 intervals, and get to 100
A=linspace(1,100,99), 99 numbers are selected from 1 to 100, which is also an arithmetic sequence
Newline expression: use...
Scalar and vector operations: just use regular operators
Vector and vector operations: a dot before the operator, such as .+, .*, ./, .^ means one-to-one correspondence operation, if there is no dot, the operation will follow the matrix operation rules
Matrix foundation
Matrix definition:
A=[1,2;2,3] or A=[1 2;2 3]
Multiple expressions: A=[1:2:11;0:5:25;linspace(10,60,6);6 5 4 6 3 2]
>> A=[1:2:11;0:5:25;linspace(10,60,6);6 5 4 6 3 2]
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
Zero matrix: zeros(4,3) four rows and three columns
One matrix: ones(4,3)
Diagonal matrix: eye(5)
>> eye(5)
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
Matrix transpose: A'
The semicolon expression of the matrix:
>> A
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
>> A(:,1:3),A(2:4,1:end),A(2:4,1:end-1)
ans =
1 3 5
0 5 10
10 20 30
6 5 4
ans =
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
ans =
0 5 10 15 20
10 20 30 40 50
6 5 4 6 3
Discrete value:
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
>> A(1:3,[1 3])
ans =
1 5
0 10
10 30
Condition value:
>> A(A>10)
ans =
20
30
15
40
20
50
11
25
60
Deletion of matrix elements: A(:,2:4)=[]
The splicing of the matrix: C=[AB]
Commonly used matrix built-in functions
Create a diagonal matrix:
v=[2 4 7],v=diag(v)
v =
2 4 7
v =
2 0 0
0 4 0
0 0 7
Reverse access to focus elements:
>> v=rand(10,5),v=diag(v)
v =
0.2760 0.7513 0.8407 0.3517 0.0759
0.6797 0.2551 0.2543 0.8308 0.0540
0.6551 0.5060 0.8143 0.5853 0.5308
0.1626 0.6991 0.2435 0.5497 0.7792
0.1190 0.8909 0.9293 0.9172 0.9340
0.4984 0.9593 0.3500 0.2858 0.1299
0.9597 0.5472 0.1966 0.7572 0.5688
0.3404 0.1386 0.2511 0.7537 0.4694
0.5853 0.1493 0.6160 0.3804 0.0119
0.2238 0.2575 0.4733 0.5678 0.3371
v =
0.2760
0.2551
0.8143
0.5497
0.9340
Change the matrix shape: reshape(A)
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
>> reshape(A,3,8)
ans =
1 6 20 10 7 6 50 25
0 3 5 30 15 9 3 60
10 5 5 4 40 20 11 2
Get matrix size: size(A)
Get the maximum value of the matrix:
%%按列取最小值,n代表该最小值在该列中第几个元素
>> [d,n]=min(A)
d =
0 3 4 6 3 2
n =
2 1 4 4 4 4
>> A
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
>> max(A)
ans =
10 20 30 40 50 60
>> max(max(A))
ans =
60
Summation: sum(A)
Sort: sort(A)
Median: medium(A)
Average: mean(A)
Standard deviation: std(A)
>> A
A =
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
6 5 4 6 3 2
>> sum(A)
ans =
17 33 49 68 82 98
>> sort(A)
ans =
0 3 4 6 3 2
1 5 5 7 9 11
6 5 10 15 20 25
10 20 30 40 50 60
>> mean(A)
ans =
4.2500 8.2500 12.2500 17.0000 20.5000 24.5000
>> std(A)
ans =
4.6458 7.8899 12.1209 15.8535 20.8886 25.4886
>> median(A)
ans =
3.5000 5.0000 7.5000 11.0000 14.5000 18.0000
Dot product: dot(A,B)
random number:
rand generates a random number of type double between 0 and 1
rand(1,5) produces a matrix of one row and five columns with arbitrary elements between 0 and 1
rand(10): Generate a matrix of 10 rows and 10 columns
randperm(10), a positive integer in the specified range
>> randperm(10)
ans =
1 5 7 3 4 6 8 9 10 2
Random numbers from standard normal distribution: randn(4,3)
Eigenvalues and eigenvectors: [v,d]=eig(A), if A is a square matrix
Matrix operations
Addition, subtraction, multiplication and division: +, -, .*, ./dot multiplication *, /cross multiplication
Matrix inversion: inv(A)
Script editor
Script definition: .m file
Script code writing:% comment
Script function writing: the use of function
Script function run: name
f is the output value, the script name must be consistent with the function name
Anonymous function:
>> f2=@(x,y)2*x^2-4*x*y+y^2;
>> f2(2,3)
ans =
-7