「研讀分享」:一看就會的圖文教程-Matlb(2)

分享興趣、傳播快樂、增長見聞、留下美好,大家好,這裡是LearningYard學苑,今天小編為大家帶來文章:一看就會的圖文教程-Matlb(2)。

「研讀分享」:一看就會的圖文教程-Matlb(2)

今日內容簡介:

在之前的文章中,已經介紹過matlab軟體的開啟介面和一些實用指南,也介紹過用matlab繪製簡單的二維圖與三維圖的方法。今天,我們著重來學習matlab中關於矩陣計算和程式碼編寫中迴圈語句的一些命令格式。

01 Matlab中關於矩陣的那些事

Matlab以“矩陣”聞名,下面我們透過一些簡單的案例理解這些常用的矩陣命令。

「研讀分享」:一看就會的圖文教程-Matlb(2)

具體的案例為:

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

「研讀分享」:一看就會的圖文教程-Matlb(2)

02條件語句

if 條件語句相對比較簡單,一般地,在下面這個條件語句中。如果滿足條件1,則執行命令1(statements1)後結束。如果不滿足 expression1也不滿足 expression2,則執行statements3。

一般的if結構為:

if expression1

statements1

elseif expression2

statements2

else

statements3

end

另外,在有多個if結構的條件語句中。MATLAB 翻譯器經常把把已知的 end 語句和它最近的 if 語句聯合在一起。

案例展示:

「研讀分享」:一看就會的圖文教程-Matlb(2)

03迴圈結構

對於一些簡單的命令,既可以用if結構實現,也可以用迴圈結構。

迴圈(loop)是一種 MATLAB 結構,它允許我們多次執行一系列的語句。迴圈結構有兩種基本形式:while 迴圈和 for 迴圈。兩者之間的最大不同在於程式碼的重複是如何控制的。在while 迴圈中,程式碼的重複的次數是不能確定的,只要滿足使用者定義的條件,重複就進行下去。相對地,在 for 迴圈中,程式碼的重複次數是確定的,在迴圈開始之前,我們就知道程式碼重複的次數了。

「研讀分享」:一看就會的圖文教程-Matlb(2)

如果 expression 的值非零(true),程式將執行程式碼塊(code block),然後返回到 while 語句執行。如果 expression 的值仍然非零,那麼程式將會再次執行程式碼。直到 expression 的值變為 0,這個重複過程結束。當程式執行到 while 語句且 expression 的值為 0 之後,程式將會執行 end 後面的第一個語句。

「研讀分享」:一看就會的圖文教程-Matlb(2)

其中 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學苑整理併發出,如有侵權,請聯絡刪除!