How to make a dictionary of arrays?

46 ビュー (過去 30 日間)
Solarmew
Solarmew 2015 年 5 月 22 日
回答済み: Geoff Hayes 2015 年 5 月 23 日
I can't seem to figure out how containers.Map works... It's doing ok with characters and numbers, but flips out when I try to feed it arrays. How do I make something like this?
function test
global a
a = containers.Map();
a(pi) = 3:14;
a(5) = 4:2:10;
end
  1 件のコメント
Solarmew
Solarmew 2015 年 5 月 22 日
oh, i think i know why ... it probably thinks i'm trying to access "pi"'th element in that array where what I'm actually trying to do is create a key a(pi) that will return 3:14 array ... Does anyone know how to fix this?

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

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 5 月 23 日
Solarmew - the default KeySet type for a container is char (see containers.Map for details) and so when you try the above, you probably see the following error message
Error using containers.Map/subsasgn
Specified key type does not match the type expected for this container.
The above error makes sense since your key of pi or 5 are of type double (or single). Since you want your keys to be doubles then just define the KeySet and _ValueType* properties as
a = containers.Map('KeyType','double','ValueType','any');
which means that
a =
Map with properties:
Count: 0
KeyType: double
ValueType: any
And so you can now use pi or 5 as a key
a(pi) = 3:14;
a(5) = 4:2:10;
Try the above and see what happens!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDictionaries についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by