dictionary - How do I use containers.Map in Matlab with a cell array as the keys and a vector of integers as the values -
i have cell array contains words 'a', 'b', , 'c' example. want able use matlab's containers.map make hash table can add values each key , able them quickly. able if not initialize containers.map object beforehand follows not allow me (or @ least haven't found way) add more key/value pairs later , makes have reinitialize object during each iteration of loop:
key = {'a','b','c'}; newmap = containers.map(key,[1,2,3]); my problem need able continually add new keys hash table , therefore cannot keep initializing containers.map object each time, want 1 long hash table keys , values while in loop.
here code trying working, want able add keys containers.map object newmap , corresponding values @ same time. keys strings in cell array , values integers:
key = {'a','b','c'}; val = [1,2,3]; newmap = containers.map(); % containers.map object initialization newmap(key) = val; my desired output this:
newmap(key) ans = 1 2 3 attempts @ solving this:
i have tried converting cell array of keys using cellstr() , char() haven't had luck these. seem keep getting error when trying this:
error using containers.map/subsref specified key type not match type expected container. thanks help.
% initialize map = containers.map('keytype','char','valuetype','double'); % assume these incrementally key = {'a','b','c'}; val = [1,2,3]; % add incrementally ii = 1:numel(key) map(key{ii}) = val(ii); end you can retrieve values @ once given set of keys, cell array. no way around it, can convert cell2mat(), or retrieve incrementally loop map(key{ii})
% retrieve values(map,key) ans = [1] [2] [3]
Comments
Post a Comment