>> a={'matlab',20;ones(2,3),1:10}
a =
'matlab' [ 20]
[2x3 double] [1x10 double]
>> b=[{'matlab'},{20};{ones(2,3)},{1:10}]
b =
'matlab' [ 20]
[2x3 double] [1x10 double]
>> c={10}
c =
[10]
>> c(1,2)={2}
c =
[10] [2]
>> c(2,2)={5}
c =
[10] [2]
[] [5]
>> isequal(a,b)
ans =
1
>> whos
Name Size Bytes Class Attributes
a 2x2 388 cell
ans 1x1 1 logical
b 2x2 388 cell
c 2x2 208 cell
用cell函数创建元胞数组,创建的数组为空元胞。cell函数创建空元胞数组的主要目的是为数组预先分配连续的存储空间,节约内存占用,提高执行效率。
>> a=cell(1)
a =
{[]}
>> b=cell(1,2)
b =
[] []
>> c=cell(3,3)
c =
[] [] []
[] [] []
[] [] []
>> d=cell(2,2,2)
d(:,:,1) =
[] []
[] []
d(:,:,2) =
[] []
[] []
>> whos
Name Size Bytes Class Attributes
a 1x1 4 cell
ans 1x1 1 logical
b 1x2 8 cell
c 3x3 36 cell
d 2x2x2 32 cell
(2)元胞数组的数据获得
从元胞数组中读取数据,可保存为一个标准的数组或一个新的单元数组,或取出数组进行计算。元胞数组中数据的访问,可通过元胞内容的下标进行,用元胞数组名加大括号{}。大括号中数值表示元胞的下标。如a{1,2}表示元胞数组中第一行第二列的元胞。