フィルターのクリア

What is the best data structure for mapping a key to an array of values in MATLAB?

6 ビュー (過去 30 日間)
Tej
Tej 2024 年 5 月 22 日
編集済み: Matt J 2024 年 5 月 23 日
I am facing an issue with which data structure to use and when. My use case involves unique keys, each of which will be mapped to an array of values.
'UniqueKey1' -> ['Value1', 'Value2', 1, 2, true];
'UniqueKey2' -> ['Value2', 'Value3', 2, 5, false];
'UniqueKey3' -> ['Value3', 'Value4', 5, 5, true];
I know Struct could be one of the options, but I am worried about the performance when the data size is large (more than 100 rows). Can someone recommend a better alternative in my case?

採用された回答

Matt J
Matt J 2024 年 5 月 22 日
編集済み: Matt J 2024 年 5 月 22 日
Dictionaries are supposed to be faster than structs, but they require a recent Matlab version.
keys="UniqueKey"+(1:3)';
values={ {'Value1', 'Value2', 1, 2, true };
{'Value2', 'Value3', 2, 5, false};
{'Value3', 'Value4', 5, 5, true}};
d=dictionary(keys,values)
d = dictionary (string --> cell) with 3 entries: "UniqueKey1" --> {1x5 cell} "UniqueKey2" --> {1x5 cell} "UniqueKey3" --> {1x5 cell}
d{"UniqueKey2"}
ans = 1x5 cell array
{'Value2'} {'Value3'} {[2]} {[5]} {[0]}
  5 件のコメント
Paul
Paul 2024 年 5 月 23 日
Try changing to:
d = dictionary;
d = insert(d, 'key1' , { {'Value1', 'Value2', 1, 2, true} } )
d = dictionary (string --> cell) with 1 entry: "key1" --> {1x5 cell}
Matt J
Matt J 2024 年 5 月 23 日
編集済み: Matt J 2024 年 5 月 23 日
You can also add new keys by direct assignment,
d = dictionary('key1',{10}, 'key2',{20})
d = dictionary (string --> cell) with 2 entries: "key1" --> {[10]} "key2" --> {[20]}
d{"key3"}={'Value1', 'Value2', 1, 2, true}
d = dictionary (string --> cell) with 3 entries: "key1" --> {[10]} "key2" --> {[20]} "key3" --> {1x5 cell}

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by