# 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``````