【005.Matlab基础 单元数组与结构体 】
来源:互联网 发布:c语言编程软件怎么用 编辑:程序博客网 时间:2024/06/06 17:35
单元数组和结构体的区别在于,结构体中的数据存储并不是由数字来标示的,而是通过结构体中的名称进行标示的。
单元数组的创建和操作
单元数组中每一个元素成为单元(cell),单元中可以包含任何类型的MATLAB数据,即可以是数组、字符、符号对象、单元数组或结构体等。
创建方法:
- 赋值语句直接创建
- 按单元索引法
- 采用标准数组的赋值方法,赋给单元的数值通过花括号({})将单元内容括起来。
- 按内容索引法
- 将花括号写在等号左边,即放在单元数组名称后
- 按单元索引法
- 通过cell函数首先为单元数组分配内存空间,然后再对每个单元进行赋值
如果工作空间内的某个变量名与所创建的单元数组同名,那么此时则不会对单元数组赋值。
>> clear A %保证赋值单元数组名称不重名>> %按单元索引法赋值>> A(1,1)={[1 3 4;2 4 6;1 4 7]};>> A(1,2)={3+5i};>> A(2,1)={'Nishad in Huai’an'}; >> A(2,2)={0:pi/5:pi};>> AA = [3x3 double] [3.0000 + 5.0000i] 'Nishad in Huai’an' [1x6 double]>> %按内容索引法赋值>> B{1,1}=[1 2 3;4 5 6;7 8 9];>> B{1,2}=2+3i;>> B{2,1}='I Love China';>> B{2,2}=1:2:13;>> BB = [3x3 double] [2.0000 + 3.0000i] 'I Love China' [1x7 double]
按单元索引法和按内容索引法所创建的单元数组是等效的。但需要注意的是,用花括号可以访问单元数组单元内容,而圆括号则只用于单元数组的单元而不显示单元数组单元的内容。如果需要将单元数组的所有内容都显示出来,则可以采用celldis函数来强制显示单元数组的所有内容。
>> B(2,2) %圆括号只用于表示单元而不显示内容ans = [1x7 double]>> B{2,2}ans = 1 3 5 7 9 11 13>> A{2,:}ans =Nishad in Huai’anans = 0 0.6283 1.2566 1.8850 2.5133 3.1416>> A(2,:)ans = 'Nishad in Huai’an' [1x6 double]
通过cell函数为单元数组赋值
>> D=cell(2,3)D = [] [] [] [] [] []>> D{1,1}=randperm(5)D = [1x5 double] [] [] [] [] []>> celldisp(D)D{1,1} = 3 5 1 2 4D{2,1} = []D{1,2} = []D{2,2} = []D{1,3} = []D{2,3} = []
单元数组函数
>> a=randn(3,4)a = 0.6277 -0.8637 -1.1135 -0.7697 1.0933 0.0774 -0.0068 0.3714 1.1093 -1.2141 1.5326 -0.2256>> b=num2cell(a,1)b = [3x1 double] [3x1 double] [3x1 double] [3x1 double]>> celldisp(b)b{1} = 0.6277 1.0933 1.1093b{2} = -0.8637 0.0774 -1.2141b{3} = -1.1135 -0.0068 1.5326b{4} = -0.7697 0.3714 -0.2256>> b=num2cell(a,2)b = [1x4 double] [1x4 double] [1x4 double]>> celldisp(b)b{1} = 0.6277 -0.8637 -1.1135 -0.7697b{2} = 1.0933 0.0774 -0.0068 0.3714b{3} = 1.1093 -1.2141 1.5326 -0.2256>> b=num2cell(a)b = [0.6277] [-0.8637] [-1.1135] [-0.7697] [1.0933] [ 0.0774] [-0.0068] [ 0.3714] [1.1093] [-1.2141] [ 1.5326] [-0.2256]>>
结构体创建
结构体(structure)和单元数组非常相似,结构体通过字段(fields)来对元素进行索引
创建方法:
- 直接赋值
- 通过struct函数创建
>> data1={3.0,5.0,'sqrt(7)'};>> data2={[0,0] [1 1] [3 5]};>> data3={'--' '...' '-.-'};>> data4={'red' 'bule' 'yellow'};>> data5={'yes' 'no' 'no'};>> circle2=struct('radius',data1,'center',data2,'linstyle',data3,... 'color',data4,'filled',data5)circle2 = 1x3 struct array with fields: radius center linstyle color filled
结构体函数
>> center=cat(1,circle2.center) %提取结构体数据后依次排列center = 0 0 1 1 3 5>> [c1,c2,c3]=deal(circle2.color)c1 =redc2 =bulec3 =yellow>> [circle2.radius]=deal(13, 27,36)circle2 = 1x3 struct array with fields: radius center linstyle color filled>> circle2.radiusans = 13ans = 27ans = 36>> fieldnames(circle2)ans = 'radius' 'center' 'linstyle' 'color' 'filled'>> isfield(circle2,'center')ans = 1>> orderfields(circles)Undefined function or variable 'circles'. Did you mean:>> orderfields(circle2)ans = 1x3 struct array with fields: center color filled linstyle radius>> circle_new=rmfiled(circle2,'filled')Undefined function or variable 'rmfiled'. Did you mean:>> circle_new=rmfield(circle2,'filled')circle_new = 1x3 struct array with fields: radius center linstyle color
下一节 字符串
0 0
- 【005.Matlab基础 单元数组与结构体 】
- Matlab基础之单元数组和结构数组
- matlab基本知识点3-单元数组与结构
- MATLAB函数句柄、单元数组和结构
- Matlab编程与数据类型 -- 单元数组
- matlab数据类型二(单元变量、结构数组)
- Matlab 结构体数组
- C++与MATLAB数组的存储结构
- Matlab编程与数据类型 -- 结构数组
- MATLAB 字符串 结构数据 单元数据
- matlab_结构数组和单元数组
- 【004.Matlab基础 数组寻址与处理 】
- Matlab-结构体数组的索引
- Matlab新建struct结构体数组
- matlab 结构(struct)数组
- matlab结构数组
- matlab结构(struct)数组
- matlab 结构(struct)数组
- GDOI2016模拟8.8旋转
- Linux 网卡驱动学习(六)(应用层、tcp 层、ip 层、设备层和驱动层作用解析)
- hdu 1010 Tempter of the Bone(搜索+奇偶剪枝)
- linux之sed用法
- HDU - 1232 - 畅通工程
- 【005.Matlab基础 单元数组与结构体 】
- Android Studio优化
- 机器学习之实战matlab神经网络工具箱
- iOS-Core Animation 核心动画
- request.getParameterValues与request.getParameter的区别
- 火星菌
- MySQL 数据库ut8mb4字符集
- 学习英语经验
- Linux常用命令002之搜索命令locate、whereis、which、find、grep