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 comentarios

Rik
Rik el 10 de Jun. de 2020
Why do you want an alternative if it does what you need?
Ganesh Kini
Ganesh Kini el 10 de Jun. de 2020
The version of the matlab that i am using does not support this function.
Could you please help me out ?
Rik
Rik el 10 de Jun. de 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
Ganesh Kini el 10 de Jun. de 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
Rik el 10 de Jun. de 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
Walter Roberson el 10 de Jun. de 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
Ganesh Kini el 10 de Jun. de 2020
Editada: Ganesh Kini el 10 de Jun. de 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)
Rik
Rik el 10 de Jun. de 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
Ganesh Kini el 10 de Jun. de 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
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
Ganesh Kini el 10 de Jun. de 2020
Editada: Walter Roberson el 7 de Mayo de 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
Rik el 10 de Jun. de 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):
  1. Keys
  2. Values
Interactions (in the context of a class: methods):
  1. isKey: check if the elements of an array are in your list of keys
  2. keys: return your list of keys
  3. length: return the number of elements in your list of keys
  4. remove: delete the specified keys and their data
  5. values: return your list of values
Ganesh Kini
Ganesh Kini el 10 de Jun. de 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
Rik el 10 de Jun. de 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
Ganesh Kini el 10 de Jun. de 2020
Editada: Ganesh Kini el 10 de Jun. de 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
Rik
Rik el 10 de Jun. de 2020
Yes, if you know all possible keys and values you can hard-code them with a switch like you described.

Iniciar sesión para comentar.

Respuestas (1)

Puru Kathuria
Puru Kathuria el 7 de Mayo de 2021

0 votos

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 comentario

Rik
Rik el 7 de Mayo de 2021
As I mentioned in this comment, I don't think a hash function is actually required. Using ismember is probably faster.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Jun. de 2020

Editada:

el 7 de Mayo de 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by