Recently, in version R2022b they announced the introduction of dictionaries.
I was under the impression that dictionaries were already available, provided by containers.Map. Are dictionaries just a different name mapped to containers.Map? Or are there other differences? I was unable to find anything comparing them online.
CodePudding user response:
From what I can gather, after reading this blog post and the comments under it, and the documentation (I haven’t yet had a chance to experiment with them, so feel free to correct me if I’m wrong):
dictionaryis an actual primitive type, likedouble,cellorstruct.containers.Mapis a “custom class”, even if nowadays the code is built-in, the functionality can never be as integrated as for a primitive type. Consequently,dictionaryis significantly faster.dictionaryuses normal value semantics. If you make a copy you have two independent dictionaries (note MATLAB’s lazy copy mechanism).containers.Mapis a handle class, meaning that all copies point to the same data, modifying one copy modifies them all.containers.Mapcan usechararrays (the old string format) or numbers as keys (stringis implicitly converted tocharwhen used as key).dictionarycan use any type, as long as it overloadskeyhash. This means you can use your own custom class objects as keys.dictionaryis vectorized, you can look up multiple values at once. With acontainers.Mapyou can look up multiple values using thevaluesfunction, not the normal lookup syntax.dictionaryhas actual O(1) lookup.If I remember correctly,*containers.Mapdoesn’t.
* No, it is also O(1), at least in R2022b.
