当前位置:网站首页>[mathematical modeling] basic knowledge of MATLAB
[mathematical modeling] basic knowledge of MATLAB
2022-07-26 04:42:00 【Qingshan's green shirt】
MATLAB Basic knowledge of
List of articles
- MATLAB Basic knowledge of
- One 、 Basic knowledge
- Two 、 data type
- 1. Numbers
- 2. Characters and strings
- 3. vector ( That's ok / As a 1 Matrix )
- 4. matrix
- 4.1sum Sum up
- 4.2 Extract the specified position elements in the matrix
- (1) Take an element of the specified row and column ( Output a value )
- (2) Take all the elements of a specified row ( The output is a line vector )
- (3) Take all the elements of a specified column ( The output is a column vector )
- (4) Take all the elements of some specified rows ( The output is a matrix )
- Take all the elements ( Spliced by column , The final output is a column vector )
- 4.3 size function
- 4.4 repmat function
- 4.5 Matrix operations
- 4.6 Find eigenvalues and eigenvectors
- 4.7 find The basic usage of function
- 4.8 Matrix and constant size judgment operation
One 、 Basic knowledge
notes :matlab Script files are used in , Suffix is .m
1. notes
%% A single line of comments ( There are upper and lower horizontal lines – Can be used to partition )
% General notes
Multiline comment : Choose , Shortcut key Ctrl+R
Cancel multiline comment : Choose , Shortcut key Ctrl+T
2. A semicolon
(1)
a = 3;
a = 5

Add a semicolon after the statement (;), Indicates that the running results are not displayed , Otherwise, there will be output , As in the above example .
(2) Semicolons can also be used for district branches .
clear;clc
3.clear and clc
clear:
clear keyword —— Delete a variable
clear —— Delete all in the workspace Variable
clc :
eliminate All text in the command line window , Keep the screen clean .
These two are usually used together , be used for ” initialization “, Prevent previous results from affecting subsequent operations / New script file
4. Input and output (disp and input)
Output :
a = 100
disp(a)
disp(' This is a string ')
b = [1 2 3]
disp(b);
% notes :disp Whether there is a semicolon after the function or not !
Input :
% Generally, we will input the number 、 vector 、 matrix 、 String, etc. to a variable , Here we give A
A = input(' Please enter A:');% It makes a difference whether you add a semicolon or not , The plus sign will have an output
B = input(' Please enter B:')%

5. Judgment statement
if The line does not need a colon , At the end of the sentence, be sure to end ending ; The middle sentence should be indented .
a = input(' Please enter the test score :')
if a >= 85
disp(' Good grades ')
elseif a >= 60
disp(' Pass the grade ')
else
disp(' Failing in grades ')
end % Indicates that the input is complete , Otherwise, you will be asked to continue to input
Two 、 data type
1. Numbers
Can perform ordinary operations Add, subtract, multiply, divide, square root, exponential logarithm , Specifically, you can find the corresponding operator in the search box in the upper right corner .
Such as :
3+3
5-1
6*6
8/8
2. Characters and strings
Use ’ ’ ( Single quotation marks ) or " " ( Double quotes ) Express
str = ' character string ‘
About string functions
(1) Merge two strings
a. function strcat(str1,str2,str3…strn)
eg:strcat(' character string 1',' character string 2')

b.[’ character string 1‘,‘ character string 2’]( Separate with commas or spaces )
(2) Convert numbers to strings
function : num2str()
c = 100
num2str(c)
% String concatenation
disp(['c The values for ' num2str(c)])
disp(strcat('c The values for ', num2str(c)))

3. vector ( That's ok / As a 1 Matrix )
Row vector
a = [1 2 3]
% perhaps ( Separate the same line with spaces or commas )
a = [1,2,3]

Column vector
% Semicolons can be used to separate the elements of each line
a = [1;2;3]

4. matrix
E = [1,2;3,4;5,6]
% Lines are separated by commas and spaces , Separate different lines with semicolons

4.1sum Sum up
Vector summation
Whether row vector or column vector , It's all direct summation
E = [1,2,3]% Row vector
sum(E)
E = [1;2;3]% Column vector
sum(E)

Matrix sum
Matrix summation needs to be distinguished according to the direction of rows and columns
E = [1 2;3 4;5 6]
% a = sum(x) ( Unspecified , Sum by column , Get a row vector )
a = sum(E)
% dim = 1 Indicates sum by column ( Get the line vector )
% dim = 2 Means sum by line ( Get the column vector )
a = sum(E,1)
a = sum(E,2)
a = sum(x(:) % Sum the entire matrix
E(:) % Extract matrix by column vector
a = sum(E(:))
% Another way
a = sum(sum(E))
a = sum(E( : ) )
4.2 Extract the specified position elements in the matrix
(1) Take an element of the specified row and column ( Output a value )
A=[1 1 4 1/3 3;1 1 4 1/3 3;1/4 1/4 1 1/3 1/2;3 3 3 1 3;1/3 1/3 2 1/3 1];
% The matrix does not have to be written in one line !
A
% The above selection is inconvenient , This makes it easy to output the entire matrix
A(2,1)
A(3,2)
(2) Take all the elements of a specified row ( The output is a line vector )
A
A(2,:)% : Means to take all elements
A(5,:)
(3) Take all the elements of a specified column ( The output is a column vector )
A
A(:,1)
A(:,3)
(4) Take all the elements of some specified rows ( The output is a matrix )
A
A([2,5],:) % Take only the second and fifth lines ( altogether 2 That's ok )
A(2:5,:) % Take the second line to the fifth line ( altogether 4 That's ok )
A(2:2:5,:) % Take the second and fourth lines ( from 2 Start , Each increment 2 A unit of , To 5 end )
% notes Generation of arithmetic sequence
1 : 3 : 10
10:-1:1 %( from 10 Start , Every time -1, To 1 end )
1:10 % What you didn't say , The difference of equal difference is 1
A(2:end,:) % Take the second line to the last line
A(2:end-1,:) % Take the second line to the penultimate line
Take all the elements ( Spliced by column , The final output is a column vector )
A
A(:)
4.3 size function
size Function usage
size(A) % (1) Used to find the size of matrix , Returns a row vector , The first element is the number of rows of the matrix , The second element is the number of columns
[r,c] = size(A) % (2) assignment take A The number of lines assigned to r A The number of columns assigned to c
r = size(A,1) % (3) Returns the number of rows
c = size(A,2 % Returns the number of columns
4.4 repmat function
B = repmat(A,m,n): The matrix A Copy m×n block , Namely the A As B The elements of ,B from m×n individual A Tiled .
A = [1,2,3;4,5,6]
B = repmat(A,3,2)

4.5 Matrix operations
Matrix addition, subtraction, multiplication and division
A + B
A - B
A * B
A / B
Find the inverse of a matrix
inv(B)
A = [1,2,3,4]
inv(A)
A/B = A*inv(B)
Multiplication and division between the corresponding elements of two matrices with the same shape
Need to use “ .*" and " ./"
A = [1,2;3,4]
B = [1,0;1,1]
A .* B
A ./ B %MATLAB in inf Infinity

Each element is multiplied or divided by a constant at the same time
All four operators can be used
A = [1,2;3,4]
A * 2 % Same as the following
A .* 2
A / 2 % Same as the following
A ./ 2
Each element multiplies simultaneously
A = [1,2;3,4]
A .^ 2
A ^ 2 % =A*A
A * A
4.6 Find eigenvalues and eigenvectors
% stay Matlab in , Calculation of matrix A The function of eigenvalues and eigenvectors of is eig(A), Two of the most commonly used uses :
A = [1 2 3 ;2 2 1;2 0 3]
% (1)E=eig(A): O matrix A All eigenvalues of , Make up a vector E.% Each eigenvalue corresponds to a set of eigenvectors
E=eig(A)
% (2)[V,D]=eig(A): O matrix A All eigenvalues of , Form a diagonal matrix D, And ask for A The eigenvectors of V The column vector .(V Every column of is D The eigenvector of the eigenvalues of the same column in )
[V,D]=eig(A)

4.7 find The basic usage of function
Used to return vectors or matrices that are not 0 The location index of the element
X = [1 0 4 -3 0 0 0 8 6]
ind = find(X)
% It has many uses , For example, before returning 2 It's not for 0 The location of the elements of :
ind = find(X,2)
% If X It's a matrix ( A two-dimensional , There are rows and columns )
X = [1 -3 0;0 0 8;4 0 6]
% return 1 3 4 8 9, Because it is regarded as a column vector
ind = find(X)
% If you need to output in the order of rows and columns
[r,c] = find(X)
[r,c] = find(X,1)% Just find the first non 0 Elements

4.8 Matrix and constant size judgment operation
X = [1 -3 0;0 0 8;4 0 6]
X > 0
X == 4

边栏推荐
- Face database collection summary
- 软考回顾及计划
- Correspondence between IEC61131 data type and C data type
- Bsdiff and bspatch incremental updates
- 数组排序1
- Optimization analysis and efficiency execution of MySQL
- Weights & biases (II)
- Throttling anti shake function of JS handwritten function
- AWS Support Plan
- Postman imports curl, exports curl, and exports corresponding language codes
猜你喜欢
随机推荐
SQL加解密注入详解
UE4 键盘控制开关灯
MySQL only checks the reasons for the slow execution of one line statements
2022 Henan Mengxin League game (3): Henan University J - magic number
egg-ts-sequelize-CLI
计算离散点的曲率(matlab)
ES6模块化+CommonJS
Weights & Biases (二)
2022河南萌新联赛第(三)场:河南大学 B - 逆序对计数
MySQL 执行失败的sql是否会计入慢查询?
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
七、RESTful
2022 Henan Mengxin League game (3): Henan University L - synthetic game
解决 Incorrect string value: ‘\xF0\x9F\x98\xAD“,...‘ for column ‘commentContent‘ at row 1 报错
What are the restrictions on opening futures accounts? Where is the safest place to open an account?
批量将PPM格式图片转化为JPG格式
Good at C (summer vacation daily question 6)
Vector explanation and iterator failure
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)
2022 Henan Mengxin League game (3): Henan University B - reverse pair count









