Reverse a map matlab converting array to string keys -


i have map defined as:

diagonal = eye(4); v = {diagonal(1,:), diagonal(2,:), diagonal(3,:), diagonal(4,:)} k = {1, 3, 7, 8} class_labels = containers.map(k, v) 

now need reverse map, matlab not allow key array , hence need convert each array string.

so class_labels map like:

1 => [0 0 0 1] 3 => [0 0 1 0] 7 => [0 1 0 0] 8 => [1 0 0 0] 

i need like:

0001  => 1 0010  => 3 0100  => 7 1000  => 8 

you can use keys , values methods associated containers.map class extract keys , values, apply string conversion values concatenating of bits together.... construct containers.map. you'll use cellfun iterate through each cell element of values cell array , apply function such converts sequence of numbers in array concatenated string.

let's assume moment don't have access keys , values defined , let's have access containers.map itself. want invert dictionary, , so:

%// code diagonal = eye(4); v = {diagonal(1,:), diagonal(2,:), diagonal(3,:), diagonal(4,:)}; k = {1, 3, 7, 8}; class_labels = containers.map(k, v);  %// new - keys , labels kr = keys(class_labels); vr = values(class_labels);  %// concatenate of bits of values string vr = cellfun(@(x) char(48+x), vr, 'uni', 0);  %// create new dictionary new_labels = containers.map(vr, kr); 

this line here confusing: vr = cellfun(@(x) char(48+x), vr, 'uni', 0);. cellfun iterates on cells in cell array , applies function each cell. function first input cellfun. declared anonymous function takes in contents of cell in cell array... array of values, adds 48 each of digits. gives array of 48/49 instead of 0/1. once this, cast array char digits represented ascii or string equivalents. ascii code 0/1 48/49. using char on modified array, produced string concatenates of these characters together. second input cell array we're working on, , third , fourth parameters tell output of cellfun not numeric vector cell array of values. 'uni' short 'uniformoutput', , set 0/false because output of function not numeric vector, cell array of vectors. each cell string created concatenating of numbers in numeric array together.

if show keys , values, get:

>> keys(new_labels)  ans =       '0001'    '0010'    '0100'    '1000'  >> values(new_labels)  ans =       [8]    [7]    [3]    [1] 

you can see each string key maps right inverse value.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -