MATLAB入门教程
MatlabGetting started
Matlab软件介绍
Matlab software introduction
Matlab和Mathematica、Maple并称为三大数学软件。它在数学类科技应用软件中在数值计算方面首屈一指。行矩阵运算、绘制函数和数据、实现算法、创建用户界面、连接其他编程语言的程序等。MATLAB的基本数据单位是矩阵,它的指令表达式与数学、工程中常用的形式十分相似,故用MATLAB来解算问题要比用C,FORTRAN等语言完成相同的事情简捷得多,并且MATLAB也吸收了像Maple等软件的优点,使MATLAB成为一个强大的数学软件。在新的版本中也加入了对C,FORTRAN,C++,JAVA的支持。
Matlab, Mathematica, and Maple are collectively called the three major mathematical software. It is second to none in numerical calculation in mathematical science and technology application software. Row matrix operations, drawing functions and data, implementing algorithms, creating user interfaces, connecting programs in other programming languages, etc. The basic data unit of MATLAB is a matrix. Its instruction expressions are very similar to those commonly used in mathematics and engineering. Therefore, it is much simpler to use MATLAB to solve problems than to use C, FORTRAN and other languages to accomplish the same thing, and MATLAB also Absorbed the advantages of software like Maple, making MATLAB a powerful mathematical software. In the new version, support for C, FORTRAN, C++, and JAVA has also been added.
Matlab界面介绍
Matlab interface introduction
1.软件下载完毕后点击“预设”——“字体”更改自己需要的页面设置。
2.主页中点击“新建”——“脚本”。新建的脚本就是当前需要编辑的地方。
3.编辑完成之后按“Ctrl+S”进行保存。
4.清除命令行窗口内容,填写“CLC”即可清除工作区内容,在命令行窗口填写“clear all”。
1. After the software is downloaded, click "Preset"-"Font" to change the page settings you need.
2. Click "New"-"Script" on the homepage. The newly created script is the place that needs to be edited currently.
3. After editing, press "Ctrl+S" to save.
4. To clear the contents of the command line window, fill in "CLC", clear the contents of the work area, and fill in "clear all" in the command line window.
Matlab数据类型
Matlab data type
1.字符与字符串
1.Characters and strings
S=’a’ ’ ’中间所表示的所有内容即为字符串
Abs(s) 表示为每个字符都有其对应的ASCII值
Char( ) 表示字符串
Num2str(65) 表示为数字65转换成为’65’
Length(str) 表示为字符串长度【其长度包括空格键】
S=’a’ ’ ’indicates everything in the middle is a string
Abs(s) means that each character has its corresponding ASCII value
Char() represents a string
Num2str(65) is expressed as the number 65 converted into ’65’
Length(str) is expressed as the length of the string [the length includes the space bar]
2.矩阵
Matrix
可用A=[1 2 3;4 5 2;3 2 7]举例
Available A=[1 2 3;4 5 2;3 2 7] example
B=A’ 表示矩阵行列将互相变换
B=A’ matrix rows and columns will be transformed into each other
C=A: 表示矩阵将竖拉一排【从第一列开始】
C=A: The matrix will be drawn vertically in one row [starting from the first column]
D=inv(A) 表示矩阵求逆【非方阵无法求逆】
E=zeros(10,5,3) 表示生成十行五列且为三位
的0矩阵命令行窗口中E=(:,:,1)即表示一维矩阵
E=(:,:,1)=rand(10,5)
rand生成均匀分布的伪函数,分布在(0~1)
之间
E=(:,:,1)=randn(10,5)
randn生成标准正态分布的伪随机数(均值为
0,方差为1)
E=(:,:,1)=randi(10,5)
randi生成均匀分布的伪随机整数
D=inv(A) Matrix inversion [Non-square
matrix cannot be inverted]
E=zeros(10,5,3) generates ten rows and
five columns with three digits 0 E=(:,:,1) in the matrix command window means a one-dimensional matrix
E=(:,:,1)=rand(10,5)
rand generates a uniformly distributed pseudo function, distributed in (0~1)
between
E=(:,:,1)=randn(10,5)
randn generates pseudorandom numbers from a standard normal distribution (the mean is0, variance is 1)
E=(:,:,1)=randi(10,5)
Randi generates uniformly distributed pseudo-random integers
3元胞数组
元胞数组是MATLAB中特有的一种数据类型,是数组的一种,其内部元素可以是属于不同的数据类型,概念理解上,可以认为它和c语言里面的结构体、c++里面的对象很类似。元胞数组是matlab中的特色数据类型,它不同于其它数据类型(如字符型,字符数组或者叫字符串,以及一般的算术数据和数组)。它特有的存取数据方法决定了它的特点,它有给人一种查询信息的感觉,可以逐渐追踪一直到所有的变量全部翻译成基本的数据信息。它的class函数输出就是cell。
Cell array is a unique data type in MATLAB. It is a kind of array. Its internal elements can belong to different data types. In terms of conceptual understanding, it can be regarded as very similar to the structure in the C language and the objects in C++. similar. Cell array is a characteristic data type in matlab, which is different from other data types (such as character type, character array or string, and general arithmetic data and array). Its unique data access method determines its characteristics. It gives people a feeling of querying information, and it can be tracked gradually until all variables are translated into basic data information. The output of its class function is cell.
设置A=cell(1,6)
Set A=cell(1,6)
A{2}=eye(3) 表示生成3x3的对角线数值为1的单位矩阵
A{2}=eye(3) generates a 3x3 identity matrix with a diagonal value of 1
A{5}=magic(5)
A{5}=magic(5)
magic(n)生成一个n阶幻方,就是把1-n^2排成一个nxn的矩阵,使得矩阵的每行、每列,以及主、副对角线上面的n个数之和都相等(容易证明,这个和等于n*(n^2+1)/2)。
magic(n) generates a magic square of order n, which is to arrange 1-n^2 into an nxn matrix, so that the sum of n numbers on each row, each column, and the main and sub diagonals of the matrix are equal (It is easy to prove that this sum is equal to n*(n^2+1)/2).
B=A{5} 即A{5}赋值给了B
B=A{5} ie A{5} is assigned to B
04结构体 Structure
Books=struct(’name’,{{Machine Learning’,’Data Mining’}},’price’,[30 40])
( )内的几位结构体,赋值给books
Books=struct(’name’,{{Machine Learning’,’Data Mining’}},’price’,[30 40])
Several structures in (), assigned to books
Books.name 在books中选取name的属性
Books.name select the attribute of name in books
Books.name(1)
Books.name{1}
#
Matlab矩阵操作
Matlab matrix operations
#
1. 矩阵的定义与构造
设置A=[1 2 3 5; 8 5 4 6]
The definition and construction of matrix
Set A=[1 2 3 5; 8 5 4 6]
B=1:2:9
其中1、2分别确定最大值 ,2为步长
B=1:2:9
Among them, 1 and 2 respectively determine the maximum value, and 2 is the step size
C=repmat(B,3,1) 重复
C=repmat(B,3,1)repeat
D=ones(2,4) 表示生成一个2行4列且值均为1的矩阵
Generate a matrix with 2 rows and 4 columns and the value is 1
矩阵的四则运算
Four arithmetic of matrix
设置A=[1 2 3 4;5 6 7 8]
B=[1 1 2 2;2 2 1 1]
C=A+B 表示对应相加
D=A-B 表示对应相减
E=A*B’ B’表示A乘以B的转置,且需要确保A行数同B列数相同
F=A.*B 表示对应项相乘
G=A/B 可以理解为G*B=A G*B*pinv(B)=A*pinv(B) G=A*pinv(B)即A*B的逆
Set A=[1 2 3 4;5 6 7 8]
B=[1 1 2 2;2 2 1 1]
C=A+B means corresponding addition
D=A-B means corresponding subtraction
E=A*B’ B’ represents the transpose of A multiplied by B, and it is necessary to ensure that the number of rows in A is the same as the number of columns in B
F=A.*B means the corresponding items are multiplied
G=A/B can be understood as G*B=A G*B*pinv(B)=A*pinv(B) G=A*pinv(B) is the inverse of A*B
矩阵的下标 Matrix subscript
设置A=magic(5) Set A=magic(5)
B=A(2,3)表示选取第二行,第三列
B=A(2,3) means to select the second row and the third column
C=A(3,:)表示选取第三行的所有列
C=A(3,:) means to select all the columns in the third row
D=A(:,4)表示选取所有行的第四列
D=A (:, 4) means to select the fourth column of all rows
[m,n]=find(A>20) 找大于20的序号值/矩阵
[m,n]=find(A>20) Find the serial number value/matrix greater than 20
Matlab逻辑与流程控制——循环结构
Matlab logic and process control-loop structure
在实际问题中,经常会遇到许多有规律的重复运算,因此,在程序设计中,需要将某些语句重复执行一组,被重复执行的语句称为循环体,每循环一次都必须做出是否继续重复执行的决定,这个决定所依据的条件称为循环的终止条件。
In actual problems, many regular repeated operations are often encountered. Therefore, in program design, certain statements need to be executed repeatedly in a group. The statements that are executed repeatedly are called loop bodies. Whether to continue to repeat the decision, the condition on which this decision is based is called the termination condition of the loop.
Matlab,提供了两种循环结构即for循环结构和while循环结构。
Matlab provides two loop structures, namely for loop structure and while loop structure.
for循环结构
for loop structure
for循环语句允许按照给定的判断范围或给定的循环次数,重复完成一次或多次运算。它从for开始,用end结束。其格式为:
for循环变量=初值:步长:终值
执行语句1
....
执行语句2
end
步长默认值为1,可省略;初值、步长、终值可以是正数也可以是负数,还可以是整数,也可以是小数,只要符合数字逻辑即可。
The for loop statement allows one or more calculations to be repeated in accordance with a given judgment range or a given number of loops. It starts with for and ends with end. The format is:
for loop variable = initial value: step length: final value
Execute statement 1
.............
Execute statement 2
end
The default value of the step length is 1, which can be omitted; the initial value, step length, and final value can be positive or negative, integer, or decimal, as long as they conform to the digital logic.
while循环结构
while loop structure
Matlab给定此结构,根据给的条件,决定是否以不确定的循环次数来执行循环语句体,基本格式为:
while条件表达式
执行语句1
......
执行语句n
end
其执行方式为若条件表达式中的条件成立,则执行循环语句,如果表达式不成立,则执行end后面的语句.
Given this structure, Matlab decides whether to execute the loop statement body with an indeterminate number of loops based on the given conditions. The basic format is:
while conditional expression
Execute statement 1
.................
Execute statement n
end
The execution method is that if the condition in the conditional expression is established, the loop statement is executed, and if the expression is not established, the statement after end is executed.
分支结构 Branch structure
1.if...end结构
if...end structure
if条件表达式
...
语句体
...
end
该结构只有一个判断语句,当条件表达式为真实,就执行与具体,如果条件表达式为假,则跳出条件题而直接执行end后面的语句
if conditional expression
...
Sentence body
...
end
This structure has only one judgment statement. When the conditional expression is true, it is executed and specific. If the conditional expression is false, the conditional question is jumped out and the statement after end is executed directly
2.if...else...end结构
if...else...end structure
if表达式
语句体1
else
语句体2
end
此时如果表达式为真,则系统将执行语句体1,如果表达式为假,则系统将执行语句体2.
if expression
Statement body 1
else
Statement body 2
end
At this time, if the expression is true, the system will execute and specific one, if the expression is false, the system will execute statement body 2
3.switch...case...end结构
switch...case...end structure
switch..case...end结构是通过与某个表达式的值进行比较,根据比较的结果做不同的选择,以实现程序的分支功能,它的结构格式为:
switch表达式(数值或字符串)
case数值或字符串1
语句体1;
case数值或字符串2
语句体2;
...
otherwise
语句体n;
end
switch后面表达式的值为数值变量或字符变量,通过这些值与case后面数值或字符串的值进行比较,与哪一个 case的值相同就执行哪 一个case下面的语句体,如果 与所有case的值都不相同,则执行otherwise 下面的语句体。otherwise 语句可以省略,如果省略otherwise,所有case都不满足时跳出分支结构,另外switch必须与end配对使用。
The switch..case...end structure is to compare with the value of a certain expression and make different choices according to the result of the comparison to realize the branch function of the program. Its structure format is:
switch expression (number or string)
case value or string 1
Sentence body 1;
case value or string 2
Sentence body 2;
...
otherwise
Sentence body n;
end
The value of the expression after switch is a numeric variable or a character variable. By comparing these values with the numeric value or string value after the case, the statement body under which case is executed if the value of which case is the same as that of all cases If the values are not the same, the statement body below otherwise is executed. The otherwise statement can be omitted. If the otherwise is omitted, the branch structure will be jumped out when all the cases are not satisfied. In addition, the switch must be paired with end.