您的位置:首页 > 编程语言 > MATLAB

Matlab-元胞数组的索引

2017-01-03 12:48 225 查看
%% 元胞数组的索引
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}
% MATLAB? creates a comma-separated list.
% Because each cell can contain a different type of data,
% you cannot assign this list to a single variable.
% However, you can assign the list to the same number of variables as cells.
% MATLAB assigns to the variables in column order
% 因为每一个元胞数组的内容不一样,所以不能将数据列在一个变量中,所以会按列的顺序分开列出所以的内容
% 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}的元胞数组操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息