官方線上課程 - Onramp

Semicolon

Adding a semicolon to the end of a command will suppress the output, though the command will still be executed.

分號可以強制輸出 output

When you enter a command without a semicolon at the end, MATLAB displays the result in the command window.

沒有用分號的話,結果會在 command window 裡。

Up Arrow in the Command Window

You can recall previous commands by pressing the Up arrow key on your keyboard. Note that the Command Window must be the active window for this to work.

在 command window 中按下向上的方向鍵,會跳出之前輸入的算式。

變數之間的關係

x = 2 y = x/2 如果我把 x 改成 1 ,y 的值是不會被影響。

The Naming Rule

You can name your MATLAB variables anything you'd like as long as they start with a letter and contain only letters, numbers, and underscores (_).

命名時要以 letter 開頭,所以 3sq = 9 是有問題的。

Clear & clc

clearing all variables by entering the command clear clear removes workspace variables, and clc clears the Command Window.

Using Built-in Functions and Constants

The operation of pi.

abs & eig

eigenvalues

sqrt

Now try using the sqrt function to calculate the square root of -9. Assign the result to a variable named z.

z = sqrt(-9)

Further Practice

Note that the solution contains the imaginary number, i, which is a built-in constant in MATLAB.

Elementary math | Descriptive statistics | Linear algebra

3.1 Manually Entering Arrays

All MATLAB variables are arrays, meaning that each variable can contain multiple elements. A single number, called a scalar, is actually a 1-by-1 array, meaning it contains 1 row and 1 column. x = 4

You can create arrays with multiple elements using square brackets.

x = [7 9]

When you separate numbers by spaces (or commas), MATLAB combines the numbers into a row vector, which is an array with one row and multiple columns (1-by-n). When you separate them by semicolons, MATLAB creates a column vector (n-by-1)

你可以在數字之間用空格或逗號分開去寫相量,他同時也算是個一列多欄的的陣列。如果你用分號的時候表示 n 乘以 1 欄的向量。

x = [1;3]

Now try creating a 1-by-3 row vector named x that contains the values 3, 10, and 5 in that order.

接著試著寫1列3行的陣列。

x =[3 10 5]

Now try creating a 3-by-1 column vector named x that contains the values 8, 2, and -4 in that order.

練習 3 列 1 行的陣列

x = [8;2;-4]

You can combine spaces and semicolons to create matrices, which are arrays with multiple rows and columns. When entering matrices, you must enter them row by row.

You can combine spaces and semicolons to create matrices, which are arrays with multiple rows and columns. When entering matrices, you must enter them row by row.

接著可以結合空格與分號來寫出矩陣(其實就是多行多列的陣列),一行一行寫。

x= [5 6 7;8 9 10]

In MATLAB, you can perform calculations within the square brackets.

在 MATLAB 內你可以直接在矩陣內進行運算

x = [sqrt(10) pi^2 ];

3.2 Creating Evenly-Spaced Vectors

It is common to create vectors containing evenly spaced numbers, such as the vector below.

x = [1 2 3]

一般最常見的向量寫法就像上面這樣,向量比陣列有彈性,可以做後續的運算。

For long vectors, entering individual numbers is not practical. An alternative, shorthand method for creating evenly spaced vectors is to use the : operator and specify only the start and end points: first:last.

y = 5:8 y = 5 6 7 8

如果是要寫連續數字的向量,就可以用冒號

The : operator uses a default spacing of 1, however you can specify your own spacing, as shown below.

x = 20:2:26 x = 20 22 24 26

冒號預設是將一開始的數字加1,然而你可以調整成加 2、加 3 等等...

If you know the number of elements you want in a vector (instead of the spacing between each element), you could instead use the linspace function: linspace(first,last,number_of_elements). Note the use of commas (,) to separate inputs to the linspace function.

如果你不想一個一個輸入向量的值,你可以用 linespace function,裡面一共有3個參數。

linspace(first,last,number_of_elements) 例如 x = linspace(1,10,5) x= 1.0000 3.2500 5.5000 7.7500 10.0000

Both linspace and the : operator create row vectors. However, you can convert a row vector into a column vector using the transpose operator .

linespace: 都可以用來寫 row vector,如果要把 row vector 變成直欄(column vector),就可以用 transpose operator(') 這個符號,是鍵盤的單引號。範例如下:

You can create column vectors in a single command by creating the row vector and transposing it all on one line. Note the use of parentheses here to specify the order of operations.

你可以把 row vector 轉成 column vectors

3.3 Array Creation Functions

results matching ""

    No results matching ""