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

`permute`

`repmat`

`reshape`

`rot90`

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