### 06 元胞数组

```元胞数组(cell array)

MATLAB数据类型。每个元胞可以具有不同的尺寸和内存空

1、使用花括号{ }，将不同类型和尺寸的数据组合在

A={zeros(2,2,2),'Hello';17.35,1:10}
A=

[2x2x2 double] 'Hello'
[
17.3500] [1x10
double]

C = {1}
C(2,2) = {3}
C=
C=
[1]
[1] []
[] [3]
2、用函数 cell 创建元胞数组。该函数可以创建一

>> A=cell(1)
A=
{[]}
>> B=cell(2,3)
B=
[] [] []
[] [] []
>> C=cell(2,2,2)
C(:,:,1) =
[] []
[] []
C(:,:,2) =
[] []
[] []
3、用[ ]由若干小元胞数组扩充成大元胞数组
A = [{zeros(2,2,2)},{'Hello'};{17.35},{1:10}]
A=
[2x2x2 double] 'Hello'
[
17.3500] [1x10 double]
B = cell(2)
B(:,1) = {char('Hello','Welcome');10:-1:5}
B=
[2x7 char ] []
[1x6 double] []
>> C=[A,B]
>> D=[A,B;C]
C=
[2x2x2 double] 'Hello'
[2x7 char ] []
[
17.3500] [1x10 double] [1x6 double] []
D=
[2x2x2 double]
[
17.3500]
[2x2x2 double]
[
17.3500]
'Hello'
[1x10 double]
'Hello'
[1x10 double]
[2x7 char ] []
[1x6 double] []
[2x7 char ] []
[1x6 double] []
4、mat2cell：将普通数组转变为元胞数组
（必须指定每个 cell 的 size，size 可以不同）
C = mat2cell(A,dim1Dist,...,dimNDist)
C = mat2cell(A,rowDist)
>> X = [1 2 3 4; 5 6 7 8; 9 10 11 12]
X=
1 2 3 4
5 6 7 8
9 10 11 12
>> Y = mat2cell(X,[1 2],[1 3])
Y=
[
1] [1x3 double]
[2x1 double] [2x3 double]
>> Y = mat2cell(X,[2,1])
Y=
[2x4 double]
[1x4 double]
1
1
2
2
1
3
1 2 3 4
5 6 7 8
9 10 11 12
1 2 3 4
5 6 7 8
9 10 11 12
5、num2cell：将普通数组转变为元胞数组
（按行或列进行转变，每个 cell 的 size 相同）
C = num2cell(A)
C = num2cell(A,dim)
>> X = [1 2 3 4; 5 6 7 8; 9 10 11 12]
X=
1 2 3 4
5 6 7 8
9 10 11 12
>> Y = num2cell(X)
Y=
[1] [ 2] [ 3] [ 4]
[5] [ 6] [ 7] [ 8]
[9] [10] [11] [12]
>> Z = num2cell(X,1)
Z=
[3x1 double] [3x1 double] [3x1 double] [3x1 double]
1
5
9
2
6
10
3
7
11
4
8
12
>> Y = num2cell(X,2)
Y=
[1x4 double]
[1x4 double]
[1x4 double]
1 2 3 4
5 6 7 8
9 10 11 12
>> M = num2cell(X,[1,2])
M=
[3x4 double]
1 2 3 4
5 6 7 8
9 10 11 12
a = ['four';'five';'nine']
c = num2cell(a)
a=
four
five
nine
c=
'f' 'o' 'u' 'r'
'f' 'i' 'v' 'e'
'n' 'i' 'n' 'e'

1.
2.
3.
4.
5.
6.

cellfun( )：将指定函数应用到元胞数组的每个元胞
7. Cell2mat()：将元胞数组转成为普通数组
1、celldisp：显示元胞数组每个元胞的内容
celldisp(C)
celldisp(C, name)：用字符串name替换元胞数组名（或ans）
>> A = {rand(2,2,2),'Hello',pi;17,1+i,magic(5)}
A=
[2x2x2 double] 'Hello'
[ 3.1416]
[
17] [1.0000 + 1.0000i] [5x5 double]
>> celldisp(A)
A{1,1} =
A{2,2} =
(:,:,1) =
1.0000 + 1.0000i
0.8147 0.1270
A{1,3} =
0.9058 0.9134
3.1416
(:,:,2) =
A{2,3} =
0.6324 0.2785
17 24 1 8
0.0975 0.5469
23 5 7 14
A{2,1} =
4 6 13 20
17
10 12 19 21
A{1,2} =
11 18 25 2
Hello
15
16
22
3
9
2、cellplot：用图形方式显示（二维）元胞数组
>> A = {rand(2,2,2),'Hello',pi;17,1+i,magic(5)}
A=
[2x2x2 double] 'Hello'
[ 3.1416]
[
17] [1.0000+ 1.0000i] [5x5 double]
>> cellplot(A)
3、元胞数组的访问（引用）
（1）使用圆括号( )访问元胞数组的元胞，返回元胞数组的子

>> A=[{zeros(2,2,2)},{'Hello'};{17.35},{1:100}]
A=
[2x2x2 double] 'Hello'
[ 17.3500] [1x100 double]
>> B=A(1,2)
B=
'Hello'
>> class(B)
ans =
cell

C = cell(4, 6);
for k = 1:24, C{k} = k * 2; end
C
C=
[2]
[4]
[6]
[8]
>> C(:,5)
ans =
[10]
[12]
[14]
[16]
[18]
[20]
[22]
[24]
[26]
[28]
[30]
[32]
[34]
[36]
[38]
[40]
[42]
[44]
[46]
[48]
[34]
[36]
[38]
[40]
（2）使用花括号{ }访问元胞数组的元胞，返回元胞数据，返

>> A=[{zeros(2,2,2)},{'Hello'};{17.35},{1:100}]
A=
[2x2x2 double] 'Hello'
[ 17.3500] [1x100 double]
>> C=A{1,2}
C=
Hello
>> class(C)
ans =
char

a Comma-Separated List（逗号分隔列表）。
Comma-Separated List常用于函数的调用参数及返回值。
C = cell(4, 6);
for k = 1:24, C{k} = k * 2; end
C
C=
[2]
[4]
[6]
[8]
[10]
[12]
[14]
[16]
[18]
[20]
[22]
[24]
[26]
[28]
[30]
[32]
[34]
[36]
[38]
[40]
[42]
[44]
[46]
[48]
>> C{:,5}
ans =
34
ans =
36
ans =
38
ans =
40
C{:,5}与C{1, 5}, C{2, 5}, C{3, 5}, C{4, 5}效果相同。
（3）花括号{ }和圆括号( )结合起来使用,访问元胞元素内部的

>> A=[{zeros(2,2,2)},{'Hello'};{17.35},{1:10}]
A=
[2x2x2 double] 'Hello'
[ 17.3500] [1x10 double]
>> D=A{1,2}(2)
D=
E
>> E=A{2,2}(5:end)
E=
5 6 7 8 9 10
>> class(E)
ans =
double
>> F=A{4}([1 3 5])
F=
1 3 5
4、元胞数组的赋值
>> C={1,2;3,4}
C=
[1] [2]
[3] [4]
>> C(1)={1:5}
>> C{2}={1:5}
>> C(:,2)={'two';'four'}
C=
[1x5 double] [2]
[
3] [4]
C=
[1x5 double] [2]
{1x1 cell } [4]
C=
[1x5 double] 'two'
{1x1 cell } 'four'
>> C(3,3)={[1,2;3,4]}
>> C(3,:)=[]
C=
[1x5 double] 'two'
[]
{1x1 cell } 'four'
[]
[]
[] [2x2 double]
C=
[1x5 double] 'two' []
{1x1 cell } 'four' []
5、元胞数组的重组
>> A = [{zeros(2,2,2)},{'Hello'};{17.35},{1:100}]
A=
[2x2x2 double] 'Hello'
[ 17.3500] [1x100 double]
>> B = reshape(A,1,4)
B=
[2x2x2 double] [17.3500] 'Hello' [1x100 double]
6、cellfun( )：将指定函数应用到元胞数组的每个元胞
(1) [A1,...,Am] = cellfun(func,C1,...,Cn)
[A1,...,Am] = cellfun(func,C1,...,Cn, 'UniformOutput',true)
func为函数句柄，这种形式要求：
①fun作用在每个元胞的输出类型(class)应为标量(scalar
values：numeric, logical, character, or structure)或元胞数组(cell
arrays)；
②fun作用在每个元胞的输出类型(class)都应相同，这个class也

>> C = {1:10, [2; 4; 6], []};
ave =
>> ave = cellfun(@mean,C)
>> L = cellfun(@isreal,C)
5.5000 4.0000
L=
1
1
1
NaN
(2) [A1,...,Am] = cellfun(func,C1,...,Cn, 'UniformOutput',false)

array)
>> C = {1:10, [2; 4; 6], []};
>> ave2 = cellfun(@mean,C, 'UniformOutput',false)
ave2 =
[5.5000] [4] [NaN]
>> b = cellfun(@sin,C,'UniformOutput',false)
b=
[1x10 double] [3x1 double] []
>> sz = cellfun(@size,C,'UniformOutput',0)
sz =
[1x2 double] [1x2 double] [1x2 double]
(3) cellfun( )的Backward Compatibility用法
A = cellfun(‘fname’,C)
fname为函数名（见下表）

isempty
islogical
isreal

length
ndims
prodofsize

size
isclass
>> A={rand(2,2,2),'Hello',pi;17,1+i,magic(5)}
A=
[2x2x2 double] 'Hello'
[ 3.1416]
[
17] [1.0000+ 1.0000i] [5x5 double]
>> B=cellfun('isreal',A)
B=
1 1 1
1 0 1
>> C=cellfun('length',A)
C=
2 5 1
1 1 5
>> D=cellfun('prodofsize',A)
D=
8 5 1
1 1 25
cellfun(‘size’,C,K)：获取元胞数组元素第K维的尺寸
cellfun(‘isclass’,C,classname)：判断元胞数组的数据类型
>> A={rand(2,2,2),'Hello',pi;17,1+i,magic(5)}
A=
[2x2x2 double] 'Hello'
[ 3.1416]
[
17] [1.0000+ 1.0000i] [5x5 double]
>> D=cellfun('size',A,1)
D=
2 1 1
1 1 5
>> E=cellfun('size',A,2)
E=
2 5 1
1 1 5
>> F=cellfun('isclass',A,'double')
F=
1 0 1
1 1 1
7、cell2mat：将元胞数组（各元胞数据类型相同，

A = cell2mat(C)
C中同一列中的每个元胞的列数必须相同
>> A = {[1] [2 3 4]; [5; 9] [6 7 8; 10 11
12]}
A=
[
1] [1x3 double]
[2x1 double] [2x3 double]
>> C = {'one','four';'six','five'}
C=
'one' 'four'
'six' 'five‘
>> B = cell2mat(A)
B=
1 2 3 4
5 6 7 8
9 10 11 12
>> D = cell2mat(C)
D=
onefour
sixfive

1、Pass the contents of a single cell by indexing with curly braces, {}.
This example creates a cell array that contains text and a 20-by-2
array of random numbers.
randCell = {'Random Data', rand(20,2)};
plot(randCell{1,2})
Random Data
1
title(randCell{1,1})
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
2
4
6
8
10
12
14
16
18
20
Plot only the first column of data by indexing further into the
content (multilevel indexing).
figure
plot(randCell{1,2}(:,1))
title('First Column of Data')
First Column of Data
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
2
4
6
8
10
12
14
16
18
20
2、Combine numeric data from multiple cells using the cell2mat function.
This example creates a 5-by-2 cell array that stores temperature data for three
cities, and plots the temperatures for each city by date.
temperature(1,:) = {'01-Jan-2010', [45, 49, 0]};
temperature(2,:) = {'03-Apr-2010', [54, 68, 21]};
temperature(3,:) = {'20-Jun-2010', [72, 85, 53]};
temperature(4,:) = {'15-Sep-2010', [63, 81, 56]};
temperature(5,:) = {'31-Dec-2010', [38, 54, 18]};
allTemps = cell2mat(temperature(:,2));
dates = datenum(temperature(:,1), 'dd-mmm-yyyy');
90
plot(dates, allTemps)
datetick('x','mmm')
80
70
60
50
40
30
20
10
0
Jan
Apr
Jul
Oct
Jan
3、Pass the contents of multiple cells as a comma-separated list to functions
that accept multiple inputs.
This example plots X against Y , and applies line styles from a 2-by-3 cell array C.
X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));
C(:,1) = {'LineWidth'; 2};
C(:,2) = {'MarkerEdgeColor'; 'k'};
C(:,3) = {'MarkerFaceColor'; 'g'};
plot(X, Y, '--rs', C{:})
3
2
1
0
-1
-2
-3
-4
-3
-2
-1
0
1
2
3
4
3、sudoku
>> magic(3)
ans =
8
3
4
1
5
9
6
7
2
X = kron(eye(3),magic(3))
k = sub2ind(size(X),[1:4,6:9],[9,8,4,3,7,6,2,1])
X(k) = [3,8,1,3,1,3,2,1]
8
1
3
5
4

7
7
7
2
5
2
1
2
64
9
8
7 8
1
64
6
4
97
2
7 8
3
6
97
2
7
4 5
64
3
6
1
4
9
2
5
2
4
97
5 6
7 8
97
5
8
7
1
64
6
2
5
2
9
6
5
64
9
8
8 9
5
8
2
7 8
3
5
8
4
64
9
8
8 9
4 5
2
4
97
1
3
4
97
6
9
7
7
9
5 6
5
6
2
5
7
7
4
97
2
5
2
3
1
4
8
6
9
5 6
2
7
4
97
64
9
6
2
2
9
5
4 5
64
3
6
8 9
2
1
3
8
1
6
3
5
7
4
9
2
8 9
5
8
7
9
5
8
tri = @(k) 3*ceil(k/3-1) + (1:3);
C = cell(9,9);
for j = 1:9
for i = 1:9
if X(i,j)==0
z = 1:9;
z(nonzeros(X(i,:))) = 0;
z(nonzeros(X(:,j))) = 0;
z(nonzeros(X(tri(i),tri(j)))) = 0;
C{i,j} = nonzeros(z)';
end
end
end
```