MATLAB
Yes isMatrix Laboratory
an abbreviation for Matrix Lab . It was produced in the late 1970s. It is an interface program written for students to use EISPACK and LINPACK when Cleve Moler, director of the Computer Department of the University of New Mexico in the United States, taught linear algebra courses to students. In 1984, it was officially introduced to the market by MathWorks and has been continuously updated and improved.MATLAB
It is a simple and easy-to-use mathematical software released by American Mathworks Corporation. It mainly faces the high-tech computing environment of scientific computing, visualization and interactive programming. It has powerful data processing capabilities and excellent graphics processing functions . Learning and mastering it will help people free themselves from heavy mathematical calculations and devote more energy to the study and research of mathematical theories.
【que】Why do you need data analysis?
[ans] Transform large volumes of complex data into actionable information.
MATLAB work interface
default layout
MATLAB R2023a
The default layout of is shown in the following figure:
- The menu bar above is a tool bar , including 文件, 变量, 代码, SIMULINK, 环境and 资源several parts.
- The right window is the command window (
Command Window
), which is used to input operation commands ; - The upper left window is the current folder , and the upper part is the path where the current folder is located.
- The lower left window is the workspace management window (
Workspace
), which displays the variable names, data structures, bytes and types of all variables currently stored in the memoryMATLAB
. Different variable types correspond to different variable name icons.
custom layout
Sometimes due to misoperation, MATLAB
the working interface of is changed. To restore to the default working interface , just change the 环境in to .布局默认
You can customize the layout 环境in in the toolbar , and you can also select the content to be displayed in . For example, if the toolbar above is missing, check in to display it.布局显示显示工具条
history window
The history record window ( Command History
), keeps the history record of all commands since installation , and marks the use time for the convenience of users to inquire. Double-click a command line to execute the command in the command window ;
call previously run statement
The command history here, if selected 弹出, the effect is to press the arrow keys on the keyboard ↑ ↓to select the previously executed statement, and press enterto select;
If selected 停靠, the effect is as shown in the figure below:
At this point, select a used command on the right, and then double-click to run the command again.
common operation
1. Clear operation
>> clc %清除命令窗口中的所有内容
>> clear %清除工作空间的所有变量
>> clear all %清除工作空间的所有变量,函数和 MEX 文件
>> clf %清除图形窗口内容
>> close %关闭当前的 Figure 窗口
>> close all %关闭所有的 Figure 窗口
These commands are generally written on the first linematlab
of the program .
Among them, the symbol %
represents a comment , and does not run in the command window.
MEX
The file is amatlab
C language (or fortran) derivative program that can be called in the environment, andmex
the compiled result is actually a dll file with the output function mexFunction.
clc effect
Compare the two pictures below to observe clc
the effect of clearing only the command window, but not the variables in the workspace :
clc
After that, the variables in the workspace are still there.
clear effect
Compare the two pictures below to observe clear
the effect of only clearing the variables in the workspace, but not clearing the command window :
clear
After that, the commands in the command line window are still there.
clf
clf help documentation
【Function description】
clf
: Deletes all children in the current figure that have a visible handle . (like theirHandleVisibility
property is set toon
)clf(fig)
: Deletes all children in the specified figure that have a visible handle .clf(‘reset’)
: Deletes all children of the current figure regardless of their handle visibility . It also resets figure properties to their default values , except forPosition
the ,Units
,PaperPosition
andPaperUnits
properties.clf(fig,‘reset’)
: Deletes all children of the specified figure and resets its properties .f = clf(…)
: A figure can be returned using either of the above syntaxes . Used when the image attribute is off, because non-integer handles are not availableIntegerHandle
when using the reset option (eg set to the default value of on).IntegerHandle
【Visible handle】If
HandleVisibility
the property of the object is set to'on'
, the handle isclf
visible to .
- When called from the Command Window or a callback routine
clf
, this function deletes only those objects whoseHandleVisibility
property is set to .'on'
- It will not delete objects whose
HandleVisibility
property is set to'callback'
or'off'
.
clf effect
close effect
close
: close the current Figure
window
2. help
- request help file
>>help 请求内容
like:
>>help elfun %关于基本函数的帮助信息
>>help exp %指数函数 exp的详细信息
- Help file in hypertext format
>>doc 请求内容
like:
>>doc elfun %以超文本格式显示关于基本函数的帮助信息
>>doc exp %以超文本格式显示关于指数函数的帮助信息
- ask for help
>>lookfor 请求内容
When you want to find an instruction with a certain function but don't know the exact namehelp
, the ability of the command is not enough, and you can use lookfor
the command at this time. lookfor
According to the complete or incomplete keywords provided by the user, a group of related instructions can be searched out.
like:
>>lookfor integral %查找有关积分的指令
>>lookfor fourier %查找能进行傅里叶变换的指令
- request a demo
>>demo 请求内容
【Summarize】
- Commands
help
,doc
, andlookfor
have their own characteristics, among whichhelp
anddoc
are only displayed differently. help
doc
The request content of , must be complete and accurate , whilelookfor
the subsequent request content can be incomplete .
Data Structures in Matlab
Create the array manually :
Matrix data access
A ( j , k ) A(j, k) A ( j ,k ) , wherejjj means thejjthline j , kkk means thekkthk列。A ( end , 1 ) A(end,1)A(end,1 ) represents the matrixAAThe data in the first column of the last row of A. A ( 1 : end , 3 ) A(1:end,3)A(1:end,3 ) represents the matrixAAThe data in the third column of A from the first row to the last row. A ( : , 3 ) A(:,3)A(:,3 ) Neutral: :: Defaults to all lines.
Built-in functions for vectorized operations
Constants and Variables
1. Constant table
MATLAB
The language itself has some predefined variable values, and these special variables with specific values are called constants .
constant | Indicates the value |
---|---|
pee peepi | pi \piPi |
i n f inf inf | positive infinity |
N to N NaNN to N | Represents an indeterminate value |
i , j i,j i,j | imaginary unit |
eps epseps | minimum number of computers |
r e a l m a x realmax realmax | largest available positive real number |
r e a l m i n realmin realmin | smallest available positive real number |
2. Variable naming rules
MATLAB
Variables in the language are composed of letters, numbers, and underscores . The main naming rules are:
- start with a letter;
- case sensitive.
3. Common forms of sentences
MATLAB
There are two most common forms of statements:
>>变量 = 表达式;
The running result is displayed as 变量 = …
.
>>表达式;
The running result is displayed as ans = …
.
Among them, ans
refers to the current calculation result . If the user does not set a variable for the expression during calculation, the system will automatically assign the current result to ans
the variable. like:
arithmetic operator
1. Addition, subtraction, multiplication, division, power
MATLAB
The input of addition, subtraction, multiplication operators is consistent with the usual computer input.- The division operation is divided into left division (
\
) and right division (/
),2/3
yes2 除以 3
, but2\3
actually is3 除以 2
. To avoid confusion, the former is taken for general division operations. - The exponentiation operator is on the computer keyboard
^
.
operator | 矩 阵the MATLAB input |
数 组the MATLAB input |
---|---|---|
add | + | + |
reduce | - | - |
take | * | .* |
remove | / | ./ |
power | ^ | .^ |
2. Matrix operations and array operations
MATLAB
The operation is divided into two types : matrix operation and array operation .
In linear algebra, a rectangular array of rows andm
columns is called a matrix. n
like:
A matrix with only 1 row or 1 column is called a vector or array.
MATLAB
The basic data unit of is a matrix , therefore, the normal operation is a matrix operation , and the operation of adding a dot before the operator is an array operation .
In MATLAB
actual operation, if you deliberately distinguish whether to add a dot before the operator, it may complicate the problem and create unnecessary confusion. Conforming to people's habitual thinking is MATLAB
a major advantage of , therefore, when operating, you can first input without adding a dot. If there is no other error in the input, but the command cannot be run, then the operation may be an array operation. Try adding before the corresponding operator .
.
like:
logical operators
Logical operation is MATLAB
an operation form of array operation in , and it is also an operation commonly used in almost all high-level languages.
symbolic operator | Function | Function name |
---|---|---|
= = == == | equal | e q eq eq |
∼ = \sim= ∼= | not equal to | no noi.e _ |
< < < | less than | l t lt lt |
> > > | more than the | g t gt gt |
< = <= <= | less than or equal to | le lel e |
> = >= >= | greater or equal to | g e ge ge |
& \& & | logic and | a n d and and |
∣ | ∣ | logical or | o r or or |
∼ \sim ∼ | logical NOT | n o t not not |
【Explanation】In arithmetic operations, comparison operations and logic AND, or, and NOT operations, their priority relations are: comparison operations, arithmetic operations, logic AND, or NOT operations .
Other common symbols
symbol | MATLAB enter |
use |
---|---|---|
comma | , |
Separate variables, expressions, columns of matrices |
semicolon | ; |
Separate command lines without displaying run results, separate rows of matrices |
apostrophe | ' ' |
define string |
colon | : |
x = a : b : c Represents a value x from a to in steps ofb c |
equal sign | = |
variable assignment |
percent sign | % |
command comment |
three periods | ... |
Continuation |
Parentheses | ( ) |
Differentiate the order of operations |
Square brackets | [ ] |
form a matrix or vector |
[Example] MATLAB
Enter matrix A in = [ 1 2 3 4 5 6 7 8 9 ] A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}A=
147258369
。
MATLAB
The procedure is as follows:
>>A = [1,2,3; 4,5,6; 7,8,9] %行与行之间用分号分隔,每行的(列)元素间用逗号分隔