---
# System prepended metadata

title: MATLAB程式設計

---

# MATLAB程式設計
## CH2 - MATLAB 基本原理
### 向量
### P17練習
```matlab=
%{第1小題}%
v = [1 5 12 19 23];
sum(v);
sum(v)/length(v);
%{第2小題}%
v2 = [1;4;6;8;9];
prod(v2);
%{第3小題}%
v3 = [4 8 9 3 6];
[val,idx] = min(v3);
%{第4小題}%
v4 = 1:100;
sum(v4);
%{第5小題}%
v5 = [2 7 9 3 1];
sort(v5, 'descend');
%{第6小題}%
v6 = linspace(0,2*pi,12);
sin(v6);

```
### P22練習
```matlab=
m = [0 3 7 2; 2 4 6 1;9 4 1 6]
%{a小題}%
size(m)
ndims(m)
%{b小題}%
numel(m)
%{c小題}%
sum(sum(m))
%{d小題}%
max(max(m))
```
### P25練習
```matlab=
v1 = [6 8 1 9 7 2 7 8];
v1(5)
v1(4:7)
length(v1)
v1(3:end)
v2 = v1(end:-1:1)
v2(5:7)=[]
v1(5:end) = 10
```
### P28練習
```matlab=
M = [1 2 3;4 5 6;7 8 9];
M(1,3)
M(2,1:2) or M(2,[1 2])
M(1,:) or M(1,1:end)
M(end, [1 3])
M = [M, [0;0;0]]
M(:,end) = []
M(1:2.1:2) or M([1 2], [1 2])

```