creating simple dictionary with an array as value without using containers.map in matlab

One way to create a simple dictionary with an array as a value without using containers.map in Matlab is to use a cell array to store the key-value pairs. Here's an example implementation:

main.m
% Initialize empty dictionary
dict = {};

% Add key-value pairs to the dictionary
dict{end+1,1} = 'key1';  % add key to first column
dict{end,2} = [1 2 3];   % add value to second column
dict{end+1,1} = 'key2';
dict{end,2} = [4 5];

% Access values in the dictionary
val1 = dict{find(strcmp(dict(:,1), 'key1')),2};
val2 = dict{find(strcmp(dict(:,1), 'key2')),2};
363 chars
13 lines

In this implementation, dict is a cell array where each row contains a key-value pair. The key is stored in the first column and the value is stored in the second column. To add a new key-value pair, we append a new row to dict with the key in the first column and the value in the second column. To access the value for a given key, we use strcmp to find the row corresponding to that key and then retrieve the value from the second column of that row.

gistlibby LogSnag