creating a dictionary with an array as value in matlab

In MATLAB, dictionaries are implemented using containers.Map. To create a dictionary with an array as value, you can follow these steps:

  1. Create the keys and values as arrays
main.m
keys = {'key1', 'key2', 'key3'};
values = {[1 2 3], [4 5 6], [7 8 9]};
71 chars
3 lines
  1. Create the dictionary using containers.Map
main.m
dictionary = containers.Map(keys, values)
42 chars
2 lines
  1. You can now access the values using their respective keys
main.m
dictionary('key1')
19 chars
2 lines

This will output:

main.m
ans =
  1     2     3
22 chars
3 lines

You can also add new key-value pairs to the dictionary using the same syntax:

main.m
dictionary('key4') = [10 11 12];
33 chars
2 lines

related categories

gistlibby LogSnag