Replacement for the function containers.Map()
显示 更早的评论
Hi All,
Hope you are doing good.
I was doing some project, I found out a function called containers.Map() and it works fine according to the logic.
I wanted to know if there is a alternative to that function ? could you please help me out ?
16 个评论
Rik
2020-6-10
Why do you want an alternative if it does what you need?
Ganesh Kini
2020-6-10
Rik
2020-6-10
If you are using a release older than R2008b you should really mention that. 11 years is a very long time in software development.
If you are commited to such an old release: be prepared to implement something like this yourself with a cell array and ismember.
Ganesh Kini
2020-6-10
Rik
2020-6-10
The point of this mapping is to have a list of keys that map to values. You can put those keys in an array and use ismember to find if your input is a key, and which position it is stored. Then you can retrieve it from your values array. So ismember can be used to implement containers.Map.
And you haven't answered a very important question: what release are you using? (and what reason do you have for not upgrading, but that is less important)
Walter Roberson
2020-6-10
containers.Map is generalized in what kinds of keys it can accept. What kinds of keys are needed for your purposes? If you are using numeric vectors as keys, for your purposes will all of the keys in any one mapping have vectors that are the same length?
Ganesh Kini
2020-6-10
编辑:Ganesh Kini
2020-6-10
Rik
2020-6-10
Are you still using Octave, like your other question? Because in that case the contents of your cell arrays are char arrays, not scalar strings, as they would be in Matlab.
Ganesh Kini
2020-6-10
Rik
2020-6-10
I just checked in Octave 5.2.0. This code works, so if your keys are all char arrays, you can use ismember to retrieve the index for your key.
keys = {"ac","bc","cc"};values = {"t_post","u_post","v_post" };
key='ac';
L=ismember(keys,key);
isKey=any(L);
if isKey,value=values{L};end
The only thing you need to do is implement a class, or write a function that does this.
Ganesh Kini
2020-6-10
编辑:Walter Roberson
2021-5-7
Rik
2020-6-10
Using if would not be a better idea than ismember.
Writing a class is not that difficult. Use an example from the documentation and adapt it until it works for you. Think about what data you need to store and what interactions with that data need to be possible:
Data (in the context of a class: private properties):
- Keys
- Values
Interactions (in the context of a class: methods):
- isKey: check if the elements of an array are in your list of keys
- keys: return your list of keys
- length: return the number of elements in your list of keys
- remove: delete the specified keys and their data
- values: return your list of values
Ganesh Kini
2020-6-10
Rik
2020-6-10
Why do you insist on using tools that are not suited for this job?
If you want to implement containers.Map you should not be using switch-case statements. If you don't want to implement it, then you can use that instead.
Either implement the class in the way I suggested, or don't implement the class at all. Apart from minor tweaks, I don't see any other way.
Ganesh Kini
2020-6-10
编辑:Ganesh Kini
2020-6-10
Rik
2020-6-10
Yes, if you know all possible keys and values you can hard-code them with a switch like you described.
回答(1 个)
Puru Kathuria
2021-5-7
0 个投票
Hi,
You can move forward by implementing your own map data structure with getters, setters and a hash function.
Otherwise, you might hardcode the data structure if all the key value pairs are known beforehand.
1 个评论
Rik
2021-5-7
As I mentioned in this comment, I don't think a hash function is actually required. Using ismember is probably faster.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!