matlab中的containers.Map()

 

matlab中的containers.Map()

标签: matlabcontainers.Map容器map
 分类:
 

目录(?)[+]

 

matlab中的containers.Map()有点类似于C++ STL中的map容器,具有key/value映射的功能。

一、新建变量

使用containers.Map()创建一个变量并初始化:

[plain] view plain copy
 
  1. % matlab  
  2. num = containers.Map({1, 2, 3}, {'one', 'two', 'three'})  

二、查看修改内容

查看num的内容:

num = 

  Map (带属性):

        Count: 3
      KeyType: double
    ValueType: char

查看num(1)的值

[plain] view plain copy
 
  1. % matlab  
  2. num(1)  
ans =

one
修改num(1)的值

[plain] view plain copy
 
  1. % matlab  
  2. num(1) = 'ONE'  

然后再查看num(1)的值

[plain] view plain copy
 
  1. % matlab  
  2. num(1)  
ans =
ONE

三、添加元素

添加元素

[plain] view plain copy
 
  1. % matlab  
  2. num(4) = 'four'  

然后再查看num(4)的值

[plain] view plain copy
 
  1. % matlab  
  2. num(4)  
ans =
four

查看num的keys值:

[plain] view plain copy
 
  1. % matlab  
  2. keys(num)  
输出:

ans = 

    [1]    [2]    [3]    [4]

查看num的values值:

[plain] view plain copy
 
  1. % matlab  
  2. values(num)  
输出:

ans = 

    'ONE'    'two'    'three'    'four'

查看num的size

[plain] view plain copy
 
  1. % matlab  
  2. size(num)  
输出:

ans =

     4     1

四、垂直串联

新建containers.Map()的第二个变量num2
[plain] view plain copy
 
  1. % matlab  
  2. num2 = containers.Map({10, 20}, {'ten', 'twenty'})  


垂直串联num和num2,containers.Map()支持垂直串联,不支持水平串联。

[plain] view plain copy
 
  1. % matlab  
  2. nummerge = [num; num2]  

查看nummerge的keys:

[plain] view plain copy
 
  1. % matlab  
  2. keys(nummerge)  
输出:
ans = 

    [1]    [2]    [3]    [4]    [10]    [20]

查看nummerge的values:

[plain] view plain copy
 
  1. % matlab  
  2. keys(nummerge)  
输出:

ans = 

    'ONE'    'two'    'three'    'four'    'ten'    'twenty'

五、删除元素

从nummerge中删除1及其对应的‘ONE’

[plain] view plain copy
 
  1. % matlab  
  2. remove(nummerge, 1)  

查看nummerge的keys:

[plain] view plain copy
 
  1. % matlab  
  2. keys(nummerge)  
输出:
ans = 

    [2]    [3]    [4]    [10]    [20]

查看nummerge的values:

[plain] view plain copy
 
  1. % matlab  
  2. keys(nummerge)  
输出:

ans = 

   'two'    'three'    'four'    'ten'    'twenty'

同时删除多个元素

[plain] view plain copy
 
  1. remove(nummerge, {2, 3})  

查看nummerge的keys:

[plain] view plain copy
 
  1. % matlab  
  2. keys(nummerge)  
输出:
ans = 

    [4]    [10]    [20]

查看nummerge的values:

[plain] view plain copy
 
  1. % matlab  
  2. keys(nummerge)  
输出:ans = 

  'four'    'ten'    'twenty'
原文地址:https://www.cnblogs.com/carl2380/p/6513823.html