Matlab-元胞数组的索引

%% 元胞数组的索引
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}的元胞数组操作

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nachifur

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值