分享兴趣、传播快乐、增长见闻、留下美好,大家好,这里是LearningYard学苑,今天小编为大家带来文章:一看就会的图文教程-Matlb(2)。

matepad使用指南(研读分享一看就会的图文教程-Matlb)(1)

今日内容简介:

在之前的文章中,已经介绍过Matlab软件的打开界面和一些实用指南,也介绍过用matlab绘制简单的二维图与三维图的方法。今天,我们着重来学习matlab中关于矩阵计算和代码编写中循环语句的一些命令格式。

01 Matlab中关于矩阵的那些事

Matlab以“矩阵”闻名,下面我们通过一些简单的案例理解这些常用的矩阵命令。

matepad使用指南(研读分享一看就会的图文教程-Matlb)(2)

具体的案例为:

matepad使用指南(研读分享一看就会的图文教程-Matlb)(3)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(4)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(5)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(6)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(7)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(8)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(9)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(10)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(11)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(12)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(13)

matepad使用指南(研读分享一看就会的图文教程-Matlb)(14)

02条件语句

if 条件语句相对比较简单,一般地,在下面这个条件语句中。如果满足条件1,则执行命令1(statements1)后结束。如果不满足 expression1也不满足 expression2,则执行statements3。

一般的if结构为:

if expression1

statements1

elseif expression2

statements2

else

statements3

end

另外,在有多个if结构的条件语句中。MATLAB 翻译器经常把把已知的 end 语句和它最近的 if 语句联合在一起。

案例展示:

matepad使用指南(研读分享一看就会的图文教程-Matlb)(15)

03循环结构

对于一些简单的命令,既可以用if结构实现,也可以用循环结构。

循环(loop)是一种 MATLAB 结构,它允许我们多次执行一系列的语句。循环结构有两种基本形式:while 循环和 for 循环。两者之间的最大不同在于代码的重复是如何控制的。在while 循环中,代码的重复的次数是不能确定的,只要满足用户定义的条件,重复就进行下去。相对地,在 for 循环中,代码的重复次数是确定的,在循环开始之前,我们就知道代码重复的次数了。

matepad使用指南(研读分享一看就会的图文教程-Matlb)(16)

如果 expression 的值非零(true),程序将执行代码块(code block),然后返回到 while 语句执行。如果 expression 的值仍然非零,那么程序将会再次执行代码。直到 expression 的值变为 0,这个重复过程结束。当程序执行到 while 语句且 expression 的值为 0 之后,程序将会执行 end 后面的第一个语句。

matepad使用指南(研读分享一看就会的图文教程-Matlb)(17)

其中 index 是循环变量(就是我们所熟知的循环指数),exp 是循环控制表达式。变量 index读取的是数组 expr 的行数,然后程序执行循环体(loopbody),所以 expr 有多少列,循环体就循环多少次。

在 for 和 end 之前的语句我们称之为循环体。在 for 循环运转的过程中,它将被重复的执行。

For 循环结构函数如下:

1.在 for 循环开始之时,MATLAB 产生了控制表达式

2.第一次进入循环,程序把表达式的第一列赋值于循环变量 index,然后执行循环体内

的语句。

3.在循环体的语句被执行后,程序把表达式的下一列赋值于循环变量 index,程序将再

一次执行循环体语句。

4.只要在控制表达式中还有剩余的列,步骤 3 将会一遍一遍地重复执行。

英文学习:

Today's content introduction:

In the previous article, I have introduced the opening interface of the matlab software and some practical guides, and also introduced the method of drawing simple two-dimensional and three-dimensional diagrams with matlab. Today, we will focus on learning some command formats for matrix calculations and loop statements in code writing in matlab.

Those things about matrices in Matlab

Matlab is famous for "matrix", let's understand these commonly used matrix commands through some simple cases.

Conditional statements

The if conditional statement is relatively simple, generally, in the following conditional statement. If condition 1 is met, then the execution of command 1 (statements1) ends. If neither expression1 nor expression2 is satisfied, statements3 are executed.

In addition, in conditional statements with multiple if structures. The MATLAB translator often combines the known end statement with its nearest if statement.

For some simple commands, you can either use an if structure or a loop structure.

A loop is a MATLAB structure that allows us to execute a series of statements multiple times. There are two basic forms of loop structure: while loop and for loop. The biggest difference between the two is how the code duplication is controlled. In the while loop, the number of code repetitions cannot be determined. As long as the user-defined conditions are met, the repetition will continue. In contrast, in a for loop, the number of code repetitions is definite. Before the loop starts, we know the number of code repetitions.

If the value of expression is non-zero (true), the program will execute the code block and then return to the while statement execution. If the value of expression is still non-zero, the program will execute the code again. Until the value of expression becomes 0, this repeated process ends. When the program executes to the while statement and the value of expression is 0, the program will execute the first statement after end.

Among them, index is the loop variable (the loop index as we know it), and exp is the loop control expression. The variable index reads the number of rows in the array expr, and then the program executes the loop body, so the loop body will loop as many times as there are columns in expr.

The statements before for and end are called loop bodies. While the for loop is running, it will be executed repeatedly. For loop structure functions are as follows:

1. At the beginning of the for loop, MATLAB generates the control expression

2. When entering the loop for the first time, the program assigns the first column of the expression to the loop variable index, and then executes the loop body

Statement.

3. After the statement in the loop body is executed, the program assigns the next column of the expression to the loop variable index, and the program will

Execute the loop body statement once.

4. As long as there are remaining columns in the control expression, step 3 will be repeated over and over again.

由于篇幅有限,今天的分享就到这里啦!

感兴趣的同学可以留言与小编交流,

咱们下周见!

参考资料:周一帆师兄的学习文档。

英文翻译:Google翻译。

本文由LearningYard学苑整理并发出,如有侵权,请联系删除!

,