像在Python中一样创建MATLAB字典 [英] Create a MATLAB dictionary like in Python

查看:62
本文介绍了像在Python中一样创建MATLAB字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道MATLAB中是否存在像Python中那样创建字典的方法.

I would like to know if there exists a way in MATLAB to create a dictionary like in Python.

我有几个端口名和端口类型,我想创建一个这样的字典:

I have several Port Name and Port Type and I would like to create a dictionary like this :

dict = {PortName : PortType, PortName : PortType, ...}

推荐答案

最接近的类比是 containers.Map :

The closest analogy is containers.Map:

containers.Map :将值映射到唯一键的对象

containers.Map: Object that maps values to unique keys

键是字符向量,字符串或数字.这些值可以具有任意类型.

The keys are character vectors, strings, or numbers. The values can have arbitrary types.

创建地图,您要传递 containers.Map 一个键的单元格数组和一个值的单元格数组(还有其他可选的输入参数):

To create the map you pass containers.Map a cell array of keys and a cell array of values (there are other, optional input arguments):

>> dict = containers.Map({ 'a' 'bb' 'ccc' }, { [1 2 3 4], 'Hey', {2 3; 4 5} });
>> dict('a')
ans =
     1     2     3     4
>> dict('bb')
ans =
    'Hey'
>> dict('ccc')
ans =
  2×2 cell array
    {[2]}    {[3]}
    {[4]}    {[5]}

您还可以键/值对添加到现有地图:

You can also append key-value pairs to an existing map:

>> dict('dddd') = eye(3);
>> dict('dddd')
ans =
     1     0     0
     0     1     0
     0     0     1

但是,根据您要执行的操作,可能有更多类似Matlab的方法来执行此操作.地图在Matlab中的使用没有字典在Python中的广泛.

However, depending on what you want to do there are probably more Matlab-like ways to do it. Maps are not so widely used in Matlab as dictionaries are in Python.

这篇关于像在Python中一样创建MATLAB字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆