And*_*kau 1 indexing hash matlab operator-overloading
是否可以重载subsref并subsasgn允许非整数类型的索引值?
h = Hash; #% a custom hash class to manage my data
h(100) = 'data'; #% integer is fine, if index > 0
h{'string'} #% but this fails
??? Cell contents reference from a
non-cell array object.
Run Code Online (Sandbox Code Playgroud)
可以用某种方式破解它吗?
有几个烦恼containers.Map,可以通过创建一个继承它的自定义类来解决:
classdef Hash < containers.Map
# fun
end
Run Code Online (Sandbox Code Playgroud)
在这样的类中,可以为用户操作实现各种类型的键(不仅仅是一个!!)和便利方法.此外,还可以重新定义subsref和subsasgn使用花括号和多个索引.太好了!
不需要破解.使用struct或container.Map.它们是关联数组的原生Matlab数据结构.结构由字符串索引(有一些限制).containers.Map可以通过字符串,非整数数字或其他数据类型进行索引.请参阅"help struct"和"help containers.Map".Map使用括号进行索引,因此其语法看起来像是通过其他方式索引的数组.
>> m = containers.Map(.1, 'myvalue');
>> m(.75) = 'anothervalue';
>> x = m(.1)
x =
myvalue
>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
687 次 |
| 最近记录: |