# Matlab陣列

MATLAB中所有資料型別的所有變數都是多維陣列。向量是一維陣列，矩陣是二維陣列。

## MATLAB中的特殊陣列

`zeros()`函式是用來建立一個全零的陣列 -

``````zeros(5)
``````

``````Trial>> zeros(5)

ans =

0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
0     0     0     0     0
``````

`ones()`函式建立一個所有元素為`1`的陣列 -

``````ones(4,3)
``````

``````Trial>> ones(4,3)

ans =

1     1     1
1     1     1
1     1     1
1     1     1
``````

`eye()`函式建立一個單位矩陣。

``````eye(4)
``````

``````Trial>> eye(4)

ans =

1     0     0     0
0     1     0     0
0     0     1     0
0     0     0     1
``````

`rand()`函式在`(0,1)` - 上建立均勻分布的亂數的陣列 -

``````rand(3, 5)
``````

``````Trial>> rand(3, 5)

ans =

0.8147    0.9134    0.2785    0.9649    0.9572
0.9058    0.6324    0.5469    0.1576    0.4854
0.1270    0.0975    0.9575    0.9706    0.8003
``````

## 魔方

`magic()`函式建立一個魔術方陣。這需要一個引數，指定正方形的大小。 引數必須是大於或等於`3`的標量。

``````magic(4)
``````

``````Trial>> magic(4)

ans =

16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1
``````

## 多維陣列

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

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

a =

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

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

``````a =

ans(:,:,1) =

0   0   0
0   0   0
0   0   0

ans(:,:,2) =

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

``````b = rand(4,3,2)
``````

``````Trial>> b = rand(4,3,2)

b(:,:,1) =

0.1419    0.9595    0.9340
0.4218    0.6557    0.6787
0.9157    0.0357    0.7577
0.7922    0.8491    0.7431

b(:,:,2) =

0.3922    0.0318    0.8235
0.6555    0.2769    0.6948
0.1712    0.0462    0.3171
0.7060    0.0971    0.9502
``````

`cat()`函式的語法是 -

``````B = cat(dim, A1, A2...)
``````

• `B`是建立的新陣列
• `A1``A2``...`是要連線的陣列
• `dim`是連線陣列的大小

``````a = [9 8 7; 6 5 4; 3 2 1];
b = [1 2 3; 4 5 6; 7 8 9];
c = cat(3, a, b, [ 2 3 1; 4 7 8; 3 9 0])
``````

``````c(:,:,1) =
9     8     7
6     5     4
3     2     1
c(:,:,2) =
1     2     3
4     5     6
7     8     9
c(:,:,3) =
2     3     1
4     7     8
3     9     0
``````

## 陣列函式

MATLAB提供以下函式來對陣列內容進行排序，旋轉，排列，重新成形或移位。

`length` 向量的大小或陣列的長度
`ndims` 陣列的維數
`numel` 陣列的元素數量
`size` 陣列的維度
`iscolumn` 確定輸入是否為列向量
`isempty` 確定陣列是否為空
`ismatrix` 確定輸入是否為矩陣
`isrow` 確定輸入是否為行向量
`isscalar` 確定輸入是否為標量
`isvector` 確定輸入是否為向量
`blkdiag` 從輸入引數構造塊對角矩陣
`circshift` 迴圈移位
`ctranspose` 復共軛轉置
`diag` 矩陣對角矩陣和對角線
`flipdim` 沿著指定的尺寸翻轉陣列
`fliplr` 從左到右翻轉矩陣
`flipud` 向下翻轉矩陣
`ipermute` 反轉N-D陣列的置換維度
`permute` 重新排列N-D陣列的維度
`repmat` 複製和平鋪陣列
`reshape` 重塑陣列
`rot90` 旋轉矩陣90度
`shiftdim` 移動維度
`issorted` 確定設定元素是否按排序順序
`sort` 按升序或降序排列陣列元素
`sortrows` 按升序排列行
`squeeze` 刪除單例維度
`transpose` 轉置
`vectorize` 向量化表示式

## 例子

``````x = [7.1, 3.4, 7.2, 28/4, 3.6, 17, 9.4, 8.9];
length(x)  % length of x vector
y = rand(3, 4, 5, 2);
ndims(y)    % no of dimensions in array y
s = ['Zara', 'Nuha', 'Shamim', 'Riz', 'Shadab'];
numel(s)   % no of elements in s
``````

``````ans =  8
ans =  4
ans =  23
``````

``````a = [1 2 3; 4 5 6; 7 8 9]  % the original array a
b = circshift(a,1)         %  circular shift first dimension values down by 1.
c = circshift(a,[1 -1])    % circular shift first dimension values % down by 1
% and second dimension values to the left % by 1.
``````

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

b =
7     8     9
1     2     3
4     5     6

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

## 排序陣列

``````v = [ 23 45 12 9 5 0 19 17]  % horizontal vector
sort(v)                      % sorting v
m = [2 6 4; 5 3 9; 2 0 1]    % two dimensional array
sort(m, 1)                   % sorting m along the row
sort(m, 2)                   % sorting m along the column
``````

``````v =
23    45    12     9     5     0    19    17
ans =
0     5     9    12    17    19    23    45
m =
2     6     4
5     3     9
2     0     1
ans =
2     0     1
2     3     4
5     6     9
ans =
2     4     6
3     5     9
0     1     2
``````

## 單元陣列

``````C = cell(dim)
C = cell(dim1,...,dimN)
D = cell(obj)
``````

• `C`是單元陣列;
• `dim`是一個整數或整數向量，它指定單元格陣列`C`的維數;
• `dim1，...，dimN`是指定`C`大小的標量整數;
• `obj`是以下之一：
• Java陣列或物件
• 型別為`System.String``System.Object`的.NET陣列

``````c = cell(2, 5);
c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5}
``````

``````c =
{
[1,1] = Red
[2,1] =  1
[1,2] = Blue
[2,2] =  2
[1,3] = Green
[2,3] =  3
[1,4] = Yellow
[2,4] =  4
[1,5] = White
[2,5] =  5
}
``````

## 存取單元格陣列資料

• 將第一個括號`()`中的索引包圍，以參照單元格集
• 將大括號`{}`中的索引括起來，以參照單個單元格內的資料

``````c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c(1:2,1:2)
``````

``````ans =
{
[1,1] = Red
[2,1] =  1
[1,2] = Blue
[2,2] =  2
}
``````

``````c = {'Red', 'Blue', 'Green', 'Yellow', 'White'; 1 2 3 4 5};
c{1, 2:4}
``````

``````ans = Blue
ans = Green
ans = Yellow
``````