Can a Dictionary Key be a char?
31 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Playing with the new dictionary in 2022b and discovered that a char can't be used as the key (can use a string though)
string.empty
dictionary(string.empty,double.empty)
char.empty
dictionary(char.empty,double.empty)
5 comentarios
Bruno Luong
el 18 de Sept. de 2022
Editada: Bruno Luong
el 18 de Sept. de 2022
"Except that the first two examples make a dictionary with key type string"
Does it matter for user? It is string internally only, but user can still add, inquire delete using char key with such dictionary.
TMW makes great effort to make many function works for both string and char-array. So internally of dictionaly class the constructor converts char-key to string, so the method of the class can be single branch. I state it clearly in my answer.
char array is historical data type (*) and has simplicity but lack of formal sophisticated object property as string. At your level I'm still surprise you bump into the difficulty to understand the difference between char.empty and string.empty.
(*) it hacks the second dimension to store "string" or must be contains in cell which is another top level totally different.
Respuestas (3)
Image Analyst
el 17 de Sept. de 2022
You have to put the character or character array into a cell because the keys are in a cell array. Watch:
keys = {'a'; 'b'; 'c'}
values = [1; 2; 3]; % A column vector of doubles.
d = dictionary(keys, values) % Create the dictionary.
% Find value when key is 'b'
val = d({'b'}) % Pass in 'b' but put it in braces to make it a cell.
See the FAQ for a good discussion of cell arrays: https://matlab.fandom.com/wiki/FAQ#What_is_a_cell_array?
2 comentarios
Bruno Luong
el 18 de Sept. de 2022
Not IMO, such detail make the doc filled with unteresting details.
Bruno Luong
el 17 de Sept. de 2022
Editada: Bruno Luong
el 17 de Sept. de 2022
It looks to me that dictionary can accept both char array and string indisrtinctively as keys, as it convert to string internally, but one can add, inquire key in either format. >So it is justt flexible kind of interface.
d=dictionary(string.empty,[])
d('a')=pi
d("a")
d('a')
Nothing prevent you to use exclusively char array of length-1 (so a char) as key. Internally it will associate or equivalent to string array of length-1 strings.
4 comentarios
Bruno Luong
el 18 de Sept. de 2022
Editada: Bruno Luong
el 18 de Sept. de 2022
"Why wouldn't there be way for Matalb to distinguish those two cases? "
Obviousmy because they want the two types to be interchagable as much as possible, if you have followed they deveoptment of string and char. You might not be happy but it's their line of development.
Explanation of "one instance"
emptyc = char.empty
stringofemptyc = string(emptyc)
length(stringofemptyc)
So
the equivalent of emptyc in string is NOT string.empty but "". This object is one instance (scalar if you will) of class string.
There is NO strict equivalent of char-array of string.empty, or put it that way, it's NOT char.empty but
charempty = char(zeros([0 0 0]))
string(charempty)
Bruno Luong
el 18 de Sept. de 2022
Editada: Bruno Luong
el 18 de Sept. de 2022
The syntax that needs to be used is
d = dictionary(char.empty(0,0,0), [])
0 comentarios
Ver también
Categorías
Más información sobre Desktop en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!