当前位置:网站首页>Matlab基础(3)——元胞与结构体
Matlab基础(3)——元胞与结构体
2022-07-30 11:28:00 【Myster_KID】
Matlab基础(3)——元胞与结构体
Matlab中的特殊变量允许用户 将不同但相关的数据类型集成一个单一的变量,以便数据的管理,类似C++中的结构体。
单元型变量(元胞)
单元型变量是以“单元”为元素的数组,每个单元可以包含各种类型的数据(如矩阵、字符串),通过{}创建,通过下标直接引用。
数组类型为cell,其中每个元素的类型也为cell。
a = 1:10;
b = 'test';
c = [1+2i,1
1,1+2i];
ce = {a, b, c};
可以通过cell()函数预先分配空间,再对其中的元素进行逐个赋值。
| 指令 | 效果 |
|---|---|
cell(n) | 生成 n × n n\times n n×n阶空单元数组 |
cell(m,n)/cell([m,n]) | 生成 m × n m\times n m×n阶空单元数组 |
cell(m,n,p,...)/cell([...]) | 生成 m × n × p × ⋯ m\times n\times p\times\cdots m×n×p×⋯阶空单元数组 |
cell(size(X)) | 生成与矩阵 X X X同维的空单元数组 |
有关单元型变量的函数:可以通过lookfor cell查找学习
| 函数 | 说明 |
|---|---|
cellfun(func,C) | 对单元型变量中的每个元素依次执行函数func |
celldisp(C) | 在命令行中逐个输出每个元素的具体内容 |
cellplot(C) | 用彩色图形窗口逐个显示元素的内容 |
num2cell(num) | 将数值转换为单元型变量 |
deal | 输入输出处理 |
cell2struct(C) | 将单元型变量转换为结构型变量 |
struct2cell(St) | 将结构型变量转换为单元型变量 |
iscell(X) | 判断是否为单元型变量 |
reshape(X,[...]) | 将 X X X中的元素按列取出,再按列重构为[]规定的维度 |
结构型变量
结构型变量是根据属性名field组织起来的不同数据类型的集合,每个属性可以包含不同的数据类型,如字符串、矩阵等,类似字典。通过函数struct来创建,通过属性名来引用属性值,通过索引来引用相应元素。
st = struct('name',{'Tom','Amy'}, 'sex',{'male','female'}, 'age',{18});
st(1); % 每个属性的第一个值 name:'Tom', sex:'male', age:18
st.sex; % 所有的sex属性 ans='male', ans='female'
st(2).name; % name属性的第二个值 ans='Amy'
创建结构型变量时,要求每个属性的长度一致,或者为标量(只有一个值),如上述的name和sex长度一致,age是标量。
有关结构型变量的函数:可以通过lookfor struct查找学习
| 函数 | 说明 |
|---|---|
fieldnames(st) | 返回结构型变量的所有属性名 |
getfield(st,fieldName) | 返回指定属性名的所有属性值 |
setfield(st,fieldName,value) | 设定指定属性名的值为value |
rmfield(st,fieldName) | 删除指定属性 |
isfield(st,fieldName) | 判断fieldName是不是st的属性 |
isstruct(st) | 判断st是否是结构型变量 |
边栏推荐
- 【JZ64 求1+2+3+...+n】
- Explain the problem of change exchange in simple terms - the shell of the backpack problem
- The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust
- MySQL——数据库基础
- LeetCode_235_Last Common Ancestor of Binary Search Tree
- NLP领域的最新研究进展
- PanGu-Coder: 函数级的代码生成模型
- RY-D1/1 Voltage Relay
- 域名怎么注册备案解析?
- 不用if分支对同一个变量做判断的方法
猜你喜欢

LeetCode_235_二叉搜索树的最近公共祖先

Redis 主从复制

Introduction to IoT Technologies: Chapter 6

RY-D1/1 Voltage Relay

High energy output!Tencent's internal MyCat middleware manual, both theoretical and practical

24. 两两交换链表中的节点

程序环境和预处理(详解)

EA中的业务对象和业务实体你分得清吗?

Verilog grammar basics HDL Bits training 08

周鸿祎:微软抄袭了360安全模式 所以成为美国最大的安全公司
随机推荐
STM32F1读取MLX90632非接触式红外温度传感器
《跟唐老师学习云网络》 - 问题定位 - 主机通但容器不通
Static LED display developed by single chip microcomputer
External Force Estimation Based on Time Delay Estimation with Perturbed Kalman Filter
Horizontal comparison of 5 commonly used registration centers, whether it is used for interviews or technical selection, is very helpful
汇编实现冒泡排序
[ASP.NET Core] Dependency Injection for Option Classes
编译Hudi
contentDocument contentWindow, canvas, svg, iframe
Farmers on the assembly line: I grow vegetables in a factory
Based on sliding mode control of uncertain neutral system finite time stable
单片机开发之LCD1602显示实验
Apifox 生成接口文档 教程与操作步骤
UE5 GAS Study Notes Postscript 0
Native js create table
TensorFlow自定义训练函数
【云筑共创】华为云携手鸿蒙,端云协同,培养创新型开发者
数字量输入输出模块DAM-5088
TensorFlow自定义训练函数
明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级