Is there any concept like dictionary or hash tables in matlab like in Python?

조회 수: 131 (최근 30일)
help me with this.
  댓글 수: 1
Sinan Islam
Sinan Islam 2022년 12월 16일
Matlab introduced dictionary in R2022b
https://www.mathworks.com/help/matlab/dictionary.html

댓글을 달려면 로그인하십시오.

채택된 답변

Mike Croucher
Mike Croucher 2022년 9월 15일
  댓글 수: 3
Bill Tubbs
Bill Tubbs 2024년 9월 4일
Do the new dictionaries allow any type of hashable object as a key or only certain types, e.g. string, int, float?

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Lateef Adewale Kareem
Lateef Adewale Kareem 2017년 2월 13일
편집: Walter Roberson 2017년 2월 13일
Yes.
c = containers.Map
c('foo') = 1
c(' not a var name ') = 2
keys(c)
values(c)

Amit
Amit 2014년 1월 22일
There is container.map http://www.mathworks.com/help/matlab/ref/containers.mapclass.html which is similar to dictionary in python.

Vikram Raghu
Vikram Raghu 2019년 4월 4일
struct
Works exactly the way I needed
  댓글 수: 6
Walter Roberson
Walter Roberson 2022년 10월 14일
time_struct_dict_table();
I added support for timing table() accesses. This is for the case where each table variable contains a single scalar -- not the best use of table() but it is interesting to compare performance.
table() is enough slower that I cannot execute here on Answers as it exceeds the time limit. The results on my desktop are:
no operation: mean = 0.00007099, med = 0.00006009, std = 0.00003329
1000 random: mean = 0.00122110, med = 0.00123992, std = 0.00010826
first field: mean = 0.00024605, med = 0.00022060, std = 0.00006604
middle field: mean = 0.00035495, med = 0.00033114, std = 0.00015423
last field: mean = 0.00027000, med = 0.00024307, std = 0.00008898
fixed dynamic: mean = 0.00143618, med = 0.00148110, std = 0.00027858
s2 no operation: mean = 0.00011520, med = 0.00011546, std = 0.00000465
s2 1000 random: mean = 0.00146808, med = 0.00140250, std = 0.00024633
s2 first field: mean = 0.00023687, med = 0.00021517, std = 0.00005892
s2 middle field: mean = 0.00012845, med = 0.00009055, std = 0.00005018
s2 last field: mean = 0.00012263, med = 0.00010719, std = 0.00004112
s2 fixed dynamic: mean = 0.00091966, med = 0.00091064, std = 0.00010529
dictionary no operation: mean = 0.00006795, med = 0.00005281, std = 0.00002384
dictionary 1000 random: mean = 0.00053217, med = 0.00052016, std = 0.00004246
dictionary first field: mean = 0.00046640, med = 0.00046126, std = 0.00002666
dictionary middle field: mean = 0.00044165, med = 0.00043643, std = 0.00001592
dictionary last field: mean = 0.00043974, med = 0.00043391, std = 0.00001467
dictionary fixed dynamic: mean = 0.00050596, med = 0.00048529, std = 0.00004669
dictionary2 no operation: mean = 0.00005400, med = 0.00005221, std = 0.00000597
dictionary2 1000 random: mean = 0.00045773, med = 0.00044854, std = 0.00002464
dictionary2 first field: mean = 0.00050153, med = 0.00049391, std = 0.00005518
dictionary2 middle field: mean = 0.00044765, med = 0.00043619, std = 0.00002839
dictionary2 last field: mean = 0.00046707, med = 0.00043328, std = 0.00007996
dictionary2 fixed dynamic: mean = 0.00045607, med = 0.00045069, std = 0.00002115
table no operation: mean = 0.00005341, med = 0.00005138, std = 0.00000502
table 1000 random: mean = 0.09879826, med = 0.09191914, std = 0.02014155
table first field: mean = 0.07515565, med = 0.06741315, std = 0.02106712
table middle field: mean = 0.08363042, med = 0.06921861, std = 0.03160464
table last field: mean = 0.06500776, med = 0.05959190, std = 0.01808197
table fixed dynamic: mean = 0.08840865, med = 0.09286350, std = 0.00894378
table2 no operation: mean = 0.00005393, med = 0.00005415, std = 0.00000115
table2 1000 random: mean = 0.03217866, med = 0.03195556, std = 0.00051499
table2 first field: mean = 0.01194878, med = 0.01183239, std = 0.00052255
table2 middle field: mean = 0.01216132, med = 0.01192055, std = 0.00058499
table2 last field: mean = 0.01306926, med = 0.01159898, std = 0.00409964
table2 fixed dynamic: mean = 0.03837043, med = 0.03305997, std = 0.01260309
Those table() results are pretty poor compared to struct or dictionary! 75 to 175 times slower !
Walter Roberson
Walter Roberson 2024년 9월 5일
It appears that in the meantime, speeds have pretty much doubled.
Unless, that is, I happened to execute the code before on my older iMac ?
R2024b Pre-release timing on my intel iMac:
no operation: mean = 0.00003648, med = 0.00003586, std = 0.00000213
1000 random: mean = 0.00053238, med = 0.00053929, std = 0.00001971
first field: mean = 0.00012308, med = 0.00012347, std = 0.00000589
middle field: mean = 0.00012308, med = 0.00012172, std = 0.00000508
last field: mean = 0.00012482, med = 0.00012419, std = 0.00000332
fixed dynamic: mean = 0.00043523, med = 0.00043637, std = 0.00000451
s2 no operation: mean = 0.00003281, med = 0.00003290, std = 0.00000043
s2 1000 random: mean = 0.00038221, med = 0.00038425, std = 0.00001155
s2 first field: mean = 0.00006217, med = 0.00006207, std = 0.00000052
s2 middle field: mean = 0.00005924, med = 0.00005918, std = 0.00000085
s2 last field: mean = 0.00005553, med = 0.00005549, std = 0.00000101
s2 fixed dynamic: mean = 0.00037049, med = 0.00036893, std = 0.00000855
dictionary no operation: mean = 0.00003666, med = 0.00003632, std = 0.00000189
dictionary 1000 random: mean = 0.00056722, med = 0.00055974, std = 0.00001518
dictionary first field: mean = 0.00052580, med = 0.00051858, std = 0.00001562
dictionary middle field: mean = 0.00052420, med = 0.00051482, std = 0.00001838
dictionary last field: mean = 0.00050794, med = 0.00050233, std = 0.00001180
dictionary fixed dynamic: mean = 0.00057878, med = 0.00057393, std = 0.00001463
dictionary2 no operation: mean = 0.00003649, med = 0.00003652, std = 0.00000030
dictionary2 1000 random: mean = 0.00051898, med = 0.00051190, std = 0.00001326
dictionary2 first field: mean = 0.00055092, med = 0.00053418, std = 0.00008305
dictionary2 middle field: mean = 0.00051139, med = 0.00051072, std = 0.00000163
dictionary2 last field: mean = 0.00052065, med = 0.00051705, std = 0.00001230
dictionary2 fixed dynamic: mean = 0.00052063, med = 0.00051347, std = 0.00001601
table no operation: mean = 0.00003769, med = 0.00003737, std = 0.00000212
table 1000 random: mean = 0.04622849, med = 0.04621893, std = 0.00149274
table first field: mean = 0.04062485, med = 0.04034006, std = 0.00122831
table middle field: mean = 0.04080905, med = 0.04076088, std = 0.00183228
table last field: mean = 0.03929389, med = 0.03886761, std = 0.00162468
table fixed dynamic: mean = 0.04706765, med = 0.04702064, std = 0.00166777
table2 no operation: mean = 0.00003161, med = 0.00003153, std = 0.00000066
table2 1000 random: mean = 0.00463795, med = 0.00463153, std = 0.00006766
table2 first field: mean = 0.00166517, med = 0.00165833, std = 0.00002508
table2 middle field: mean = 0.00161921, med = 0.00162277, std = 0.00003672
table2 last field: mean = 0.00167462, med = 0.00167515, std = 0.00002166
table2 fixed dynamic: mean = 0.00462260, med = 0.00460212, std = 0.00009719

댓글을 달려면 로그인하십시오.


Bill Tubbs
Bill Tubbs 2020년 4월 17일
편집: Bill Tubbs 2020년 4월 17일
struct seems to produce a much nicer text output than container.Map:
% Example with struct
settings = struct();
settings.open_loop = false;
settings.adaptive = true;
settings.estimator = 'RLSFF';
if settings.open_loop
do something...
end
>> disp(settings)
open_loop: 0
adaptive: 1
estimator: 'RLSFF'
>> settings
settings =
struct with fields:
open_loop: 0
adaptive: 1
estimator: 'RLSFF'
% Example with Map
settings2 = containers.Map;
settings2('open_loop') = false;
settings2('adaptive') = true;
settings2('estimator') = 'RLSFF';
if settings2('open_loop')
do something...
end
>> settings2
settings2 =
Map with properties:
Count: 3
KeyType: char
ValueType: any
>> disp(settings)
open_loop: 0
adaptive: 1
estimator: 'RLSFF'
Although they look identical when returning at the command line.
But is there a literal representation for a struct? So you can define it in code more concisely, something like this:
settings = struct(
'open_loop': false,
'adaptive': true,
'estimator': 'RLSFF'
);
(The above is not valid of course). Or is there some other way of doing this in a readable convenient way?
  댓글 수: 1
Bill Tubbs
Bill Tubbs 2020년 4월 20일
Your example doesn't work. Unfortunately, you need to add the line continuation syntax:
settings = struct(...
'open_loop', false, ...
'adaptive', true, ...
'estimator', 'RLSFF' ...
);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dictionaries에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by