# Matlab變數

``````x = 12       % defining x and initializing it with a value
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> x = 12       % defining x and initializing it with a value

x =

12
``````

``````x = sqrt(16)     % defining x and initializing it with an expression
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> x = sqrt(16)     % defining x and initializing it with an expression

x =

4
``````

• 當變數輸入到系統中，可以在接下來程式碼中參照。
• 變數在使用前必須有值。
• 當表示式返回未分配給任何變數的結果時，系統將其分配給名為`ans`的變數，稍後可以使用它。

``````sqrt(99)
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> sqrt(99)

ans =

9.9499
``````

``````sqrt(99);
99.499/ans
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> sqrt(99);
99.499/ans

ans =

10.0000
``````

``````x = 7 * 8;
y = x * 7.89
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> x = 7 * 8;
y = x * 7.89

y =

441.8400
``````

## 多重分配賦值

``````a = 2; b = 7; c = a * b
``````

MATLAB執行上述語句並返回以下結果 -

``````c = 14
``````

## 變數歷史

`who`命令顯示使用過的所有變數名。

``````Trial>> who

ans  x    y
``````

`whos`命令更多地顯示變數 -

• 當前在記憶體中的變數
• 每個變數的型別
• 每個變數的記憶體分配
• 是否是複合的變數？

``````Trial>> whos
Name      Size            Bytes  Class     Attributes

ans       1x1                 8  double
x         1x1                 8  double
y         1x1                 8  double
``````

``````clear x     % it will delete x, won't display anything
clear          % it will delete all variables in the workspace
%  peacefully and unobtrusively
``````

## 長任務

``````initial_velocity = 0;
acceleration = 9.8;
time = 20;
final_velocity = initial_velocity ...
+ acceleration * time
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> initial_velocity = 0;
acceleration = 9.8;
time = 20;
final_velocity = initial_velocity + acceleration * time

final_velocity =

196
``````

## 格式命令

`format long`命令顯示十進位制後的`16`位數位。

``````Trial>> format long
x = 7 + 10/3 + 5 ^ 1.2

x =

17.231981640639408
``````

``````Trial>> format short
x = 7 + 10/3 + 5 ^ 1.2

x =

17.2320
``````

`format bank`命令將數位舍入到小數點後兩位。例如，

``````format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> format bank
daily_wage = 177.45;
weekly_wage = daily_wage * 6

weekly_wage =

1064.70
``````

MATLAB使用指數符號顯示大數位。

`format short e`命令以指數形式顯示四位小數加上指數。

``````format short e
4.678 * 4.9
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> format short e
4.678 * 4.9

ans =

2.2922e+01
``````

`format long e`命令允許以指數形式顯示十六位小數加上指數。 例如，

``````format long e
x = pi
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> format long e
x = pi

x =

3.141592653589793e+00
``````

`format rat`命令給出計算結果最接近的合理表示式。 例如，

``````format rat
4.678 * 4.9
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> format rat
4.678 * 4.9

ans =

2063/90
``````

## 建立向量

• 行向量
• 列向量

``````r = [7 8 9 10 11]
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> r = [7 8 9 10 11]

r =

7              8              9             10             11
``````

``````r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t

res =

9             11             13             15             17
``````

``````c = [7;  8;  9;  10; 11]
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> c = [7;  8;  9;  10; 11]

c =

7
8
9
10
11
``````

## 建立矩陣

``````m = [1 2 3; 4 5 6; 7 8 9]
``````

MATLAB執行上述語句並返回以下結果 -

``````Trial>> m = [1 2 3; 4 5 6; 7 8 9]

m =

1              2              3
4              5              6
7              8              9
``````

## 以下是糾正/補充內容：

shift加迴車可以換行，非要非要非要非要20字  提交時間：2019-08-31