01 基本操作與數(shù)組輸入
Intro
認(rèn)識(shí) MATLAB 操作界面
可以通過 Layout 選擇需要顯示的窗口及布局
使用 MATLAB 編程有兩種方法
- 命令行 (命令窗口)
- 腳本 (
.m
文件)
像使用計(jì)算器一樣使用 MATLAB
-
運(yùn)算符
+
,-
,*
,/
,^
-
計(jì)算的結(jié)果 以
ans
顯示
-
優(yōu)先級(jí)
- 同一優(yōu)先級(jí)從左到右
- 優(yōu)先級(jí)為
- Parenthesis (
()
) - Power (
^
) - Multiplication and division (
*
,/
) - Addition and subtraction (
+
,-
)
- Parenthesis (
Exercise
Calculate:
cos
?
(
(
1
+
2
+
3
+
4
)
3
5
)
sin
?
(
π
)
+
ln
?
(
tan
?
(
1
)
)
2
3.5
×
1.7
e
sin
?
(
10
)
\begin{aligned} &\cos \left(\sqrt{\frac{(1+2+3+4)^{3}}{5}}\right) \\ \\ &\sin (\sqrt{\pi})+\ln (\tan (1)) \\ \\ &2^{3.5 \times 1.7} \\ \\ &e^{\sin (10)} \end{aligned}
?cos(5(1+2+3+4)3??)sin(π?)+ln(tan(1))23.5×1.7esin(10)?
嵌套函數(shù)
sin(cos(pi))
與下方等價(jià)
cos(pi)
sin(ans)
多行代碼 可以合為單行代碼執(zhí)行
MATLAB 先計(jì)算
cos(pi)
并將計(jì)算結(jié)果儲(chǔ)存到變量ans
中
在計(jì)算sin(ans)
時(shí), 與sin(cos(pi))
等價(jià)
變量
- 變量在賦值前, 不需要先聲明
- 單等號(hào)
=
是賦值運(yùn)算符(Assignment Operator)- 將等號(hào)右邊的值 賦值給等號(hào)左邊
- 大小寫敏感
- 變量命名不能以數(shù)字開始, 但可以包含數(shù)字
數(shù)據(jù)類型
logical
char
-
numeric
-
int8
,int16
,int32
,int64
,uint8
,uint16
,uint32
,uint64
single
double
-
cell
struct
who
與 whos
使用命令 who
可以查看當(dāng)前變量
使用命令 whos
可以查看當(dāng)前變量及其數(shù)據(jù)類型
>> a = 5
a =
5
>> a * 6
ans =
30
>> who
Your variables are:
a ans
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
ans 1x1 8 double
特殊變量與常量
ans
-
i,j
: complex number -
Inf
: ∞ \infty ∞ -
eps
: 2.2207 e ? 016 2.2207e-016 2.2207e?016 -
NaN
: not a number -
pi
: π \pi π
>> x = 1/0
x =
Inf
>> x = inf / inf
x =
NaN
以上均是關(guān)鍵字
使用命令 iskeyword
顯示關(guān)鍵字
>> iskeyword
ans =
20×1 cell array
{'break' }
{'case' }
{'catch' }
{'classdef' }
{'continue' }
{'else' }
{'elseif' }
{'end' }
{'for' }
{'function' }
{'global' }
{'if' }
{'otherwise' }
{'parfor' }
{'persistent'}
{'return' }
{'spmd' }
{'switch' }
{'try' }
{'while' }
標(biāo)識(shí)符查找順序
- Variable 變量
- Built-in function 內(nèi)置函數(shù)
- Subfunction 子函數(shù)
- Private function 私有函數(shù)
- MEX-file
- P-file
- M-file
例如:
>> cos='This String.';
>> cos(8) % 字符向量的第8個(gè)元素
ans =
'r'
>> clear % 清除全部變量
>> cos(8)
ans =
-0.1455
注意: 不要使用 內(nèi)置函數(shù) 或 關(guān)鍵字 作為變量名
指定數(shù)字格式
默認(rèn)數(shù)字格式為 short
顯示小數(shù)點(diǎn)后四位
使用 format [style]
指定數(shù)字格式
Style | Result | Example |
---|---|---|
short |
Short, fixed-decimal format with 4 digits after the decimal point. | 3.1416 |
long |
Long, fixed-decimal format with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values. | 3.141592653589793 |
shortE |
Short scientific notation with 4 digits after the decimal point. | 3.1416e+00 |
longE |
Long scientific notation with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values. | 3.141592653589793e+00 |
bank |
Currency format with 2 digits after the decimal point. | 3.14 |
hex |
Hexadecimal representation of a binary double-precision number. | 400921fb54442d18 |
rat |
Ratio of small integers. 以有理數(shù)形式顯示結(jié)果 | 355/113 |
命令行下使用
觀察以下兩個(gè)命令的不同
>> a = 10
>> b = 10;
命令后加 ;
表示不向終端顯示結(jié)果
與其他終端相同, 使用 ↑ \uparrow ↑ 快速輸入之前的命令
一些常用的命令
-
clc
清空命令窗口 -
clear
清除工作區(qū)的所有變量- 謹(jǐn)慎使用, 建議只刪除不需要的變量
clear [variable]
- 謹(jǐn)慎使用, 建議只刪除不需要的變量
-
who
查看工作區(qū)的變量 -
whos
查看工作區(qū)變量的詳細(xì)信息
數(shù)組輸入
Array (Vector and Matrix) Input
行向量 Row vector
>> a = [1 2 3 4]
列向量 Column vector
>> b = [1; 2; 3; 4]
>> a * b
ans =
30
>> b * a
ans =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
- Key in the following matrix in MATLAB
A = [ 1 21 6 5 17 9 31 2 7 ] A= \begin{bmatrix} 1&21&6\\ 5&17&9\\ 31&2&7 \end{bmatrix} A=? ??1531?21172?697?? ??
>> A = [1 21 6; 5 17 9; 31 2 7]
A =
1 21 6
5 17 9
31 2 7
使用下標(biāo)獲取矩陣中的元素
注意: 與其他編程語言中不同, MATLAB 中向量的下標(biāo)從 1 開始
>> A = [1 21 6; 5 17 9; 31 2 7]
A =
1 21 6
5 17 9
31 2 7
>> A(8)
ans =
9
>> A([1 3 5])
ans =
1 31 17
>> A([1 3; 1 3])
ans =
1 31
1 31
>> A(3,2)
ans =
2
>> A([1 3], [1 3])
ans =
1 6
31 7
觀察以上命令運(yùn)行的結(jié)果 可以看出 MATLAB 獲取矩陣中元素的方法
- 沒有逗號(hào)
-
A([index])
從上往下, 從左往右 對(duì)元素從 1 開始標(biāo)號(hào) -
A([1 3 5])
取出下標(biāo)為 1 3 5 的元素, 結(jié)果為數(shù)組 -
A([1 3; 1 3])
取出下標(biāo)為 1 3 的元素 放在矩陣的第一行, 再取出下標(biāo)為 1 3 的元素 放在矩陣的第二行
-
- 有逗號(hào)
-
A([row], [column])
根據(jù)(行,列)坐標(biāo)取出元素 -
A([1 3], [1 3])
取出 ([行 行], [列 列]) 交點(diǎn)上的元素 放入矩陣, 也就是 第 1, 3 行 與 第 1,3 列 交點(diǎn)上的元素
-
Exercise
完成以下賦值
A
=
[
1
21
6
5
17
9
31
2
7
]
?
[
1
76
6
5
17
9
31
0
7
]
?
[
1
0
0
5
0
0
31
0
7
]
?
[
1
0
0
5
0
0
]
A=\left[\begin{array}{ccc} 1 & 21 & 6 \\ 5 & 17 & 9 \\ 31 & 2 & 7 \end{array}\right] \Longrightarrow\left[\begin{array}{ccc} 1 & 76 & 6 \\ 5 & 17 & 9 \\ 31 & 0 & 7 \end{array}\right]\Longrightarrow\left[\begin{array}{ccc} 1 & 0 & 0 \\ 5 & 0 & 0 \\ 31 & 0 & 7 \end{array}\right]\Longrightarrow \left[\begin{array}{lll} 1 & 0 & 0 \\ 5 & 0 & 0 \end{array}\right]
A=?
??1531?21172?697??
????
??1531?76170?697??
????
??1531?000?007??
???[15?00?00?]
提示 請(qǐng)先看完下面的冒號(hào)運(yùn)算符
A(3,:)
表示選中第三列的全部A(3,:)=[]
讓第三列等于一個(gè)空向量, 就是刪除第三列
冒號(hào)運(yùn)算符
-
如果想要?jiǎng)?chuàng)建這樣的數(shù)組: A = [ 1 2 3 ? 100 ] A = \begin{bmatrix}1&2&3&\cdots&100\end{bmatrix} A=[1?2?3???100?]
-
可以使用冒號(hào)運(yùn)算符(Colon Operator)
-
語法
j : k ? [ j , ? j + 1 , ? j + 2 , ? ? , ? j + m ] j : i : k ? [ j , ? j + i , ? j + 2 i , ? ? , j + m × i ] \begin{array}{l} j:k&\Longrightarrow&[j,\ j+1,\ j+2,\cdots,\ j+m]\\ j:i:k&\Longrightarrow&[j,\ j+i,\ j+2i,\cdots,j+m\times i] \end{array} j:kj:i:k????[j,?j+1,?j+2,?,?j+m][j,?j+i,?j+2i,?,j+m×i]?start:length:end
未指定步長時(shí), 默認(rèn)步長為 1
A
=
[
1
2
3
?
100
]
A = \begin{bmatrix}1&2&3&\cdots&100\end{bmatrix}
A=[1?2?3???100?] 可以使用 A=[1:100]
創(chuàng)建
矩陣拼接
相同形狀的矩陣可以連接在一起
例如:
>> A=[1 2; 3 4];
>> B=[9 9; 9 9];
>> F=[A B]
F =
1 2 9 9
3 4 9 9
>> F=[A; B]
F =
1 2
3 4
9 9
9 9
矩陣運(yùn)算
- 可以用在矩陣上的運(yùn)算符 :
+
,-
,*
,/
,^
,.
,'
對(duì)以下矩陣:
A
=
[
1
2
3
4
5
4
9
8
7
]
B
=
[
3
3
3
2
4
9
1
3
1
]
a
=
2
A=\left[\begin{array}{lll} 1 & 2 & 3 \\ 4 & 5 & 4 \\ 9 & 8 & 7 \end{array}\right] \quad B=\left[\begin{array}{lll} 3 & 3 & 3 \\ 2 & 4 & 9 \\ 1 & 3 & 1 \end{array}\right] \quad a=2
A=?
??149?258?347??
??B=?
??321?343?391??
??a=2
做如下運(yùn)算:
>> A
A =
1 2 3
4 5 4
9 8 7
>> B
B =
3 3 3
2 4 9
1 3 1
>> a
a =
2
>> A+a
ans =
3 4 5
6 7 6
11 10 9
>> A/a
ans =
0.5000 1.0000 1.5000
2.0000 2.5000 2.0000
4.5000 4.0000 3.5000
>> A./a
ans =
0.5000 1.0000 1.5000
2.0000 2.5000 2.0000
4.5000 4.0000 3.5000
>> A^a
ans =
36 36 32
60 65 60
104 114 108
>> A.^a
ans =
1 4 9
16 25 16
81 64 49
>> A'
ans =
1 4 9
2 5 8
3 4 7
>> A+B
ans =
4 5 6
6 9 13
10 11 8
>> A*B
ans =
10 20 24
26 44 61
50 80 106
>> A.*B
ans =
3 6 9
8 20 36
9 24 7
>> A/B
ans =
0.0714 0.2857 0.2143
1.1667 0 0.5000
3.2619 -0.2857 -0.2143
>> A./B
ans =
0.3333 0.6667 1.0000
2.0000 1.2500 0.4444
9.0000 2.6667 7.0000
.
加上運(yùn)算符, 表示矩陣對(duì)應(yīng)元素間的運(yùn)算 而不是矩陣之間的運(yùn)算'
表示對(duì)矩陣求轉(zhuǎn)置
注意: 矩陣不能相除, 這里矩陣間的除法運(yùn)算 與
A
×
B
?
1
A\times B^{-1}
A×B?1 的結(jié)果大概相等
也就是表示對(duì)一個(gè)空間, 先進(jìn)行
A
A
A 線性變化, 然后再進(jìn)行
B
B
B 線性變化的逆變化
這里涉及到了 線性代數(shù)的本質(zhì)
簡要概括為:
- 矩陣
- 形式上看, 是一個(gè)數(shù)表
- 本質(zhì)上是對(duì)空間施加線性變化
- 對(duì)矩陣求逆(
inv()
)之后再相乘, 表示“還原變化”- 行列式
- 形式上看, 是一個(gè)數(shù)
- 本質(zhì)上是經(jīng)過 行列式對(duì)應(yīng)矩陣 所代表的線性變化后
線性空間內(nèi)圖形的 長度(1維)/面積(2維)/體積(3維)/… 變化的倍數(shù)- 對(duì)矩陣求對(duì)應(yīng)行列式的值:
det()
一些特殊的矩陣
-
linespace()
: -
eye(n)
: 主對(duì)角線上全是 1 1 1, 其他地方全為 0 0 0 的 n × n n\times n n×n 矩陣 -
zeros(n1,n2)
: n 1 × n 2 n_1\times n_2 n1?×n2? 零矩陣 -
ones(n1,n2)
: n 1 × n 2 n_1\times n_2 n1?×n2? 單位矩陣 -
diag()
: 輸入數(shù)組, 會(huì)將數(shù)組元素放在對(duì)角線上, 其他元素全為零 -
rand()
: 生成隨機(jī)矩陣
矩陣相關(guān)的函數(shù)
-
max(A)
找出 A 矩陣中每一列最大的元素 結(jié)果為行向量- 嵌套使用可以找出 A 矩陣中最大的元素
max(max(A))
- 嵌套使用可以找出 A 矩陣中最大的元素
-
min()
用法與max()
一樣 -
sum(A)
對(duì) A 每一列的元素求和 結(jié)果為行向量- 嵌套使用可以得到 A 矩陣中所有元素的和
-
mean(A)
對(duì) A 的每一列求平均數(shù) 結(jié)果為行向量- 嵌套使用可以得到 A 矩陣中所有元素的平均值
-
sort(A)
對(duì) A 的每一列進(jìn)行排序, 從上到下遞增- 是列操作
-
sortrows(A)
對(duì) A 的第一列進(jìn)行排序 同時(shí)把對(duì)應(yīng)列所在行也進(jìn)行移動(dòng)-
例如
-
>> A=[1 2 3; 0 5 6; 7 0 9] A = 1 2 3 0 5 6 7 0 9 >> sortrows(A) ans = 0 5 6 1 2 3 7 0 9
-
-
-
size(A)
得到 A 矩陣的階數(shù) 結(jié)果為兩個(gè)數(shù) 第一個(gè)數(shù)為行數(shù) 第二個(gè)數(shù)為列數(shù) -
length(A)
得到 A 向量的長度文章來源:http://www.zghlxwxcb.cn/news/detail-455391.html -
find(A==[num])
返回 A 矩陣中等于[num]
的元素的下標(biāo)文章來源地址http://www.zghlxwxcb.cn/news/detail-455391.html
到了這里,關(guān)于【MATLAB】 01 基本操作與數(shù)組輸入的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!