matlab基础操作(五)

31.数组维数的减小

>> a=magic(4),a(:,2)=[]

>> a(1,2)=[]

带有下标的赋值维度不匹配。

>> a(2:4)=[]%数组a将变为向量

32.元胞数组的创建

Cell indexing方式创建元胞数组

>> c(1,1)={[1 4 3;0 5 8;7 2 9]}

>> c(1,2)={'Anne Smith'}

>> c(2,1)={3+7i}

>> c(2,2)={-pi:pi/10:pi}

>> class(c)

Content indexing方式创建元胞数组

>> b{1,1}='James'

>> class(b)

>> b{1,2}=[1 2;3 4;5 6]

>> b{2,1}=pi

>> b{2,2}=zeros(5)

33.元胞数组的连接

>> a=[b c]

>> a=[b;c]

34.显示元胞数组的内容

>> a(2,2)={-pi:pi/10:pi}

>> celldisp(a)%显示全部内容

>> cellplot(a)%图形方式显示元胞数组的结构

>> a{1,2}%使用内容下标索引显示指定元胞的数据

>> a{:}%一次显示a的全部数据

>> b,d=b{1,2}%读取b元胞数组的第1行、第2列元胞的内容

>> e=b{1,2}(3,1)%读取b{1,2}的第3行、第1列的数据

>> f=a(1,:)%读取元胞数组a第一行的所有元胞

>> a(1,:)=[]%删除元胞数组a第一行的所有元胞

35.结构数组变量的创建

方法一:直接键入

Exam:创建一个关于学生信息的结构数组,每个结构包含学生姓名(name)、学号(id)、成绩(scores)。

>> clear student%清除student变量

>> student.name='张三';%加入name字段

>> student.id='mr871912';%加入id字段

>> student.scores=[58,75,62];%加入scores字段

>> student%显示结构变量的数据

>> student(2).name='张宁';

>> student(2).id='mr871913';

>> student(2).scores=[68,85,92];

>> student

>> student(1)

方法二:struct函数

格式:Structure Array_var_name=struct(field1,value1,field2,value2,...),field1、field2、...是结构的字段名,value1、value2、...

则是相应字段所包含的数据。

Exam:使用struct创建结构数组变量

>> clear student

>> student=struct('name','张听说','scores',[50 60]);

>> student(2)=struct('name','张延安','scores',[60 70]);

>> student(1),student(2)

Exam:使用struct创建结构数组变量(一次建立多个元素)

>> clear student

>> student=struct('name',{'张婷说','张延安'},'scores',{[50 60],[60 70]});

>> student(1),student(2)

相关推荐
freexyn1 天前
Matlab自学笔记六十一:快速上手解方程
数据结构·笔记·matlab
ytttr8731 天前
matlab通过Q学习算法解决房间路径规划问题
学习·算法·matlab
梦子要转行2 天前
matlab/Simulink-全套50个汽车性能建模与仿真源码模型9
开发语言·matlab·汽车
Zevalin爱灰灰2 天前
MATLAB GUI界面设计 第六章——常用库中的其它组件
开发语言·ui·matlab
曹勖之11 天前
simuilink和ROS2数据联通,Run后一直卡在Initializting
windows·matlab·simulink·ros2
Zevalin爱灰灰12 天前
MATLAB GUI界面设计 第三章——仪器组件
开发语言·ui·matlab
算法如诗12 天前
基于SOA(海鸥优化算法)的路径规划Matlab实现方案
开发语言·算法·matlab
项目申报小狂人12 天前
2025年中科院三区全新算法,恒星振荡优化器:受自然启发的元启发式优化,完整MATLAB代码免费获取
开发语言·算法·matlab
Zevalin爱灰灰13 天前
MATLAB GUI界面设计 第二章——APP Designer操作正式入门
开发语言·ui·matlab
季截13 天前
07、python调用matlab引擎
matlab