# Matlab矩陣

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

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

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

a =

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

## 參照矩陣中的元素

``````mx(m, n);
``````

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(2,5)
``````

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

``````Trial>> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(2,5)

ans =

6
``````

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)
``````

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

``````Trial>> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
v = a(:,4)

v =

4
5
6
7
``````

``````a(:,m:n)
``````

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)
``````

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

``````Trial>> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)

ans =

2     3
3     4
4     5
5     6
``````

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)
``````

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

``````Trial>> a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(:, 2:3)

ans =

2     3
3     4
4     5
5     6
``````

``````3     4     5
4     5     6
``````

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
sa = a(2:3,2:4)
``````

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

``````sa =
3     4     5
4     5     6
``````

## 刪除矩陣中的一行或一列

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a( 4 , : ) = []
``````

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

``````a =
1     2     3     4     5
2     3     4     5     6
3     4     5     6     7
``````

``````a = [ 1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
a(: , 5)=[]
``````

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

``````a =
1     2     3     4
2     3     4     5
3     4     5     6
4     5     6     7
``````

``````a = [ 1 2 3 ; 4 5 6; 7 8 9];
new_mat = a([2,3,2,3],:)
``````

``````new_mat =
4     5     6
7     8     9
4     5     6
7     8     9
``````