快捷搜索:  汽车  科技

matlab经典教程从入门到精通(周一分享三十一)

matlab经典教程从入门到精通(周一分享三十一)本期推文阅读时长大约5分钟,请您耐心阅读。欢迎您的用心访问!亲爱的您,这里是LearingYard学苑!今天小编为大家带来Matlab入门学习。

Matlab入门学习

Getting started with Matlab

matlab经典教程从入门到精通(周一分享三十一)(1)

分享兴趣,传播快乐,

增长见闻,留下美好。

亲爱的您,

这里是LearingYard学苑!

今天小编为大家带来Matlab入门学习。

欢迎您的用心访问!

本期推文阅读时长大约5分钟,请您耐心阅读。

Share interests spread happiness

increase knowledge and leave beauty behind.

Dear you

this is LearningYard Academy!

Today the editor brings you Matlab introductory learning.

Welcome your visit!

The reading time of this tweet is about 5 minutes please read it with patience.

今天小编为大家介绍if语句,for语句,while语句。

Today I will introduce you to if statement for statement while statement.

if语句if statement

if语句是条件语句,每一个条件语句都需要end关键词结束。

The if statement is a conditional statement and each conditional statement needs to end with the end keyword.

下面举一个例子,生成一个随机数,如果小于0.3,显示“<0.3”,如果大于0.7,显示“>0.7”,其他显示“0.3~0.7”。

Let's take an example to generate a random number. If it is less than 0.3 it will display "<0.3" if it is greater than 0.7 it will display ">0.7" and the others will display "0.3~0.7".

代码如下图所示:

The code is shown below:

matlab经典教程从入门到精通(周一分享三十一)(2)

运行结果如下图所示:

The running result is shown in the following figure:

matlab经典教程从入门到精通(周一分享三十一)(3)

for语句for statement

for语句通过遍历一个数组,执行有限次的循环。

The for statement executes a finite number of loops by iterating over an array.

基本语法如下:

The basic syntax is as follows:

for 变量=初值:增量:结束值

程序模块;

end

for variable = initial value: increment: end value

program module;

end

while语句while statement

如果进入循环前,循环的次数是不确定的,要根据循环中代码的执行结果来决定何时退出,此时可用while循环。

If the number of times of the loop is uncertain before entering the loop it is necessary to decide when to exit according to the execution result of the code in the loop. At this time a while loop can be used.

基本语法如下:

The basic syntax is as follows:

while 条件

循环体语句

end

while condition

loop body statement

end

for与while对比for vs while

for用于循环次数确定的情况下,while多用于循环次数不确定的情况。

for is used when the number of loops is determined while is mostly used when the number of loops is uncertain.

下面举一个例子,使用for语句和while语句分别求1到100的和。

Here's an example that uses the for statement and the while statement to sum from 1 to 100 respectively.

for语句代码如下图:

The code for the for statement is as follows:

matlab经典教程从入门到精通(周一分享三十一)(4)

while语句代码如下图:

The while statement code is as follows:

matlab经典教程从入门到精通(周一分享三十一)(5)

得出的结果相同,都是5050。

The result is the same both 5050.

matlab经典教程从入门到精通(周一分享三十一)(6)

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天。

祝您今天过得开心快乐!

That's it for today's sharing.

If you have a unique idea about today’s article

welcome to leave us a message

let us meet tomorrow.

I wish you a happy day today!

参考资料:谷歌翻译、百度、哔哩哔哩

本文由LearningYard学苑原创,如有侵权请在后台留言!

文字|Zheng

排版|Zheng

审核|任务达人

猜您喜欢: