-
Cell Index
-
Content Index
>>A = [1 2 3;4 5 6;7 8 9]
B = ['abc','opq';'rst','tpl']
C = {'one','two'}
D = {101,B;A,'aha';NaN,C}
运行结果
A =
1 2 3
4 5 6
7 8 9
B =
abcopq
rsttpl
C =
'one' 'two'
D =
[ 101] [2x6 char]
[3x3 double] 'aha'
[ NaN] {1x2 cell}
cellplot(D)
元胞元素单元格(Cell)可以存储其他任意数据类型的数组
- 数值数组
- 字符数组
- 元胞数组
- 空值
单元索引 ()
% 单元索引
cell_D = D(1,1)
% cell_D 为元胞数组
D(3,2)
输出结果
cell_D =
[101]
ans =
{1x2 cell}
内容索引 {}
>>% 内容索引
num_D = C{1,1}
% num_D为数值数组
D{3,2}
输出结果
num_C =
101
ans =
'one' 'two'
总结
运算时注意变量是基础数组类型而不是元胞
复制从某篇文章
%% 了解元胞数组单元索引与内容索引
%% 元胞数组的索引
C = {'one', 'two', 'three';
1, 2, 3};
%% 1.Cell Indexing with Smooth Parentheses, () (操作数组本身)
% 1.
upperLeft = C(1:2,1:2);
% 2.
C(1,1:3) = {'first','second','third'}
% 3.If cells in your array contain numeric data, you can convert the cells to a numeric array using the cell2mat function:
% 将元胞数组中数值部分转化为矩阵
numericCells = C(2,1:3)
numericVector = cell2mat(numericCells)
%% 2.Content Indexing with Curly Braces, {} (操作数组里面的内容)
% 1.
last = C{2,3}
% 2.
C{2,3} = 300
% 3.access the contents of multiple cells
C{1:2,1:2}
[r1c1, r2c1, r1c2, r2c2] = C{1:2,1:2}
% 因为每一个元胞数组的内容不一样,所以不能将数据列在一个变量中,所以会按列的顺序分开列出所以的内容
% 4.当数据类型相同时,可以使用 [] 合并数据
nums = [C{2,:}]
% * 数组可以使用 {} 合并
% 5.元胞数组的赋值操作
[r1c1, r2c1, r1c2, r2c2] = C{1:2,1:2} % 对内容赋值
rc = {C{1:2,1:2}} % 对内容操作 + 合并内容
rc = C(1:2,1:2) % 对元胞数组进行操作赋值
r1c1 = C{1:2,1:2} % 这种操作只能将第一个赋过去
% 6.结构体中产生的元胞数组
imname = dir(['C:\Users\ncf\Desktop\' '*.doc']);%读入文件夹下的 doc ,imname 为结构体
file_all = {imname.name}; % imname.name 产生元胞数组(因为每一个文档的名字长度不同)
%% 3.多维数的操作
% 1.
myNum = [1, 2, 3];
myCell = {'one', 'two'};
myStruct.Field1 = ones(3);
myStruct.Field2 = 5*ones(5);
C = {myNum, 100*myNum;
myCell, myStruct};
C{1,2}
C{1,1}(1,2) % 操作元胞中矩阵
C{2,1}{1,2} % 操作元胞中的元胞
C{2,2}.Field2(5,1)
C{2,1}{2,2} = {pi, eps}; % 嵌入新的值, 对C{2,1}的元胞数组操作