1.map
容器类型和概述
map
:映射,将一个量映射到另一个量。将一个字符串映射成一个数值,字符串称为map
的键(key),那个值称为map
的数据(value);类似于Python
的字典
一个
map
是MATLAB
的一个对象,具有3种属性。属性查看:map
+.
+map属性名
,(mapW.ValueType
)
2.创建map
对象
map
是一个map
类中的对象,由MATLAB
中名为容器的包来定义
>> schedulemap = containers.Map({'monday','tuesday','wedesday','thursday','friday'},{'maths','chinese','history','geography','biology'})
schedulemap =
Map (具有属性):
Count: 5
KeyType: char
ValueType: charschedulemap =
创建空对象
newMap = containers.Map()
3.查看、读取map
对象
1.查看map
对象
map
对象的每个条目是一个键值对
>> keys(schedulemap)
values(schedulemap)
ans =
'friday' 'monday' 'thursday' 'tuesday' 'wedesday'
ans =
'biology' 'maths' 'geography' 'chinese' 'history'
2.读取map
对象
格式:
valueName = schedulemap(keyName)
a = schedulemap('friday')
a =
biology
多个键同时寻访,使用
values
函数
values(schedulemap,{'monday','thursday'})
ans =
'maths' 'geography'
4.编辑map
函数
1.从map
对象中删除键值对
格式:
remove('keyname','valuename')
>> remove(schedulemap,'monday')
ans =
Map (具有属性):
Count: 4
KeyType: char
ValueType: char
>> keys(schedulemap)
values(schedulemap)
ans =
'friday' 'thurday' 'thursday' 'wednesday'
ans =
'biology' 'chinese' 'geography' 'history'
2.添加键值对
schedulemap('sunday')='public class'
schedulemap =
Map (具有属性):
Count: 6
KeyType: char
ValueType: char
>> keys(schedulemap)
values(schedulemap)
ans =
'friday' 'saturday' 'sunday' 'thurday' 'thursday' 'wednesday'
ans =
'biology' 'public' 'public class' 'chinese' 'geography' 'history'
3.修改keys
不同于修改
values
,修改步骤为,先删除values
所对应的keys
,再添加values
对应的keys
>> remove(schedulemap,'saturday')
ans =
Map (具有属性):
Count: 5
KeyType: char
ValueType: char
>> schedulemap('sunday')='mba'
>> keys(schedulemap)
ans =
'friday' 'sunday' 'thurday' 'thursday' 'wednesday'
>> values(schedulemap)
ans =
'biology' 'mba' 'chinese' 'geography' 'history'
4.修改values
schedulemap('sunday')='mba'
schedulemap =
Map (具有属性):
Count: 6
KeyType: char
ValueType: char
>> values(schedulemap,{'friday','sunday'})
ans =
'biology' 'mba'