map容器类型

1.map容器类型和概述

map:映射,将一个量映射到另一个量。将一个字符串映射成一个数值,字符串称为map的键(key),那个值称为map的数据(value);类似于Python的字典

一个mapMATLAB的一个对象,具有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'
    原文作者:凉风起天末_
    原文地址: https://www.jianshu.com/p/d090be152d04
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞