You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Replacement for the function containers.Map()
9 views (last 30 days)
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
Ganesh Kini
on 10 Jun 2020
The version of the matlab that i am using does not support this function.
Could you please help me out ?
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
ismember is not the solution is not the exact solutuion that i am looking for.
Do you have any other alternative ? please let me know. I will check that
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
keys = {"ac","bc","cc"};
values = {"t_post","u_post","v_post" };
M = containers.Map(keys,values);
I have functionality where i get the keys value and when i pass it
post = M(keys);
output will look like
t_post = M(ac)
similarly 2nd index of keys should point to 2nd index of Values and so on.
What are the other ways to do it ( other than the 'if' condition)
Ganesh Kini
on 10 Jun 2020
I am using octave too and its not working since i have the older version and i cant upgrade due to the legacy code and other reasons
Is it possible to suggest a alternate solution for the above question?
It would help me a lot
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
I tried with hkeys = { "ac" } and values = { "t_post" } and it worked fine for me too.
I guess if condition would be better to implement. whats your opinion ?
I am a beginner so writing a class would be difficult for me
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
Since i need fixed values for the set of inputs. I guess i will go for case statements.
for example for ac - output should be t_post
bc - it shoukd be u_post
cc - it shoukd be v_post
Whats your opinion ?
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
I understand what you are saying. but for now since i need to get results rather the approach. So i guess case statements would work right? atleast for my problem that i am facing
Answers (1)
Puru Kathuria
on 7 May 2021
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.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)