Replacement for the function containers.Map()
Show older comments
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 Comments
Rik
on 10 Jun 2020
Why do you want an alternative if it does what you need?
Ganesh Kini
on 10 Jun 2020
Rik
on 10 Jun 2020
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
on 10 Jun 2020
Rik
on 10 Jun 2020
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
on 10 Jun 2020
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
on 10 Jun 2020
Edited: Ganesh Kini
on 10 Jun 2020
Rik
on 10 Jun 2020
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
on 10 Jun 2020
Rik
on 10 Jun 2020
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
on 10 Jun 2020
Edited: Walter Roberson
on 7 May 2021
Rik
on 10 Jun 2020
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
on 10 Jun 2020
Rik
on 10 Jun 2020
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
on 10 Jun 2020
Edited: Ganesh Kini
on 10 Jun 2020
Rik
on 10 Jun 2020
Yes, if you know all possible keys and values you can hard-code them with a switch like you described.
Answers (1)
Puru Kathuria
on 7 May 2021
0 votes
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 Comment
Rik
on 7 May 2021
As I mentioned in this comment, I don't think a hash function is actually required. Using ismember is probably faster.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!