一、
1、元胞数组的创建
>> a={'matlab',;ones(,),:} a = 'matlab' [ ]
[2x3 double] [1x10 double]>> a={'matlab',;ones(,),:}
>> b=[{'matlab'},{};{ones(,)},{:}] b = 'matlab' [ ]
[2x3 double] [1x10 double]
a和b是等价的
>> c={}
c =
[]
>> c(2,2)={5}
c =
[10] []
[] [5]
>> c(1,2)={2}
c =
[10] [2]
[] [5]
clear all a={'matlab',;ones(,),:}
b=[{'matlab'},{};{ones(,)},{:}]
c={}
c(,)={}
c(,)={}
c(,)=
赋值需用花括号,下标用小括号。
>> isequal(a,b) ans =
>> whos
Name Size Bytes Class Attributes a 2x2 cell
ans 1x1 logical
b 2x2 cell
c 2x2 cell
>> d=cell(,) d = [] [] []
[] [] []
[] [] []
2、元胞数组访问
>> str=a(,) str = 'matlab' >> class(str) ans = cell
>> str=a{,} str = matlab >> class(str) ans = char
>> a{,} ans = >> a(,) ans = [1x10 double]
()和{}有着本质的区别,大括号用于表示元胞的内容,小括号表示指定的元胞。
>> a(,)()
错误: ()-索引必须显示在索引表达式的最后。 >> a{,}() ans =
>> b=c{:,:} b = >> b=c(:,:) b = [] []
[] []
b相当于从c中获取了四个元胞,用a的例子更好理解
>> d=a(,:) d = [2x3 double] [1x10 double]
>> d=a(,) d = [1x10 double] >> d=a{,} d =
3、元胞数组的删除和重塑
要删除单元数组中的行或列,可以用冒号表示单元数组中的行或列,然后对其赋一个空矩阵即可。
>> a={20,'matlab';ones(2,3),1:3} a = [ 20] 'matlab'
[2x3 double] [1x3 double]
>> a{1,2}=[]
a =
[ 20] []
[2x3 double] [1x3 double]
>> a{1,1}=[]
a =
[] []
[2x3 double] [1x3 double]
>> a={,'matlab';ones(,),:} a = [ ] 'matlab'
[2x3 double] [1x3 double] >> a() ans = [2x3 double] >> a() ans = 'matlab'
可以看出四个元胞数组的排序
>> a{}=[] a = [ ] 'matlab'
[2x3 double] [] >> a()=[] a = [] [2x3 double] 'matlab'
删除一个之后的重新排序。