I would like to map numeric output from a matrix to a string.
Given
compute=[ 7, 4, 3; 3, 4, 7]
how can one obtain a string mappin
MATLAB has a Map container type that makes this very straighftorward:
keySet = [7, 4, 3];
valSet = {'Run', 'Walk', 'Jog'};
map = containers.Map(keySet,valSet);
Get the requested values:
>> vals = values(map,num2cell(compute))
vals =
'Run' 'Walk' 'Jog'
'Jog' 'Walk' 'Run'
This is a class after all, so you can also use a more familiar OOP syntax for calling the values method:
>> vals = map.values(num2cell(compute))
vals =
'Run' 'Walk' 'Jog'
'Jog' 'Walk' 'Run'
You can use cell array
strs = {'one','two','Jog','Walk','five','six','Run'};
compute=[ 7, 4, 3; 3, 4, 7];
out = strs(compute);
out =
'Run' 'Walk' 'Jog'
'Jog' 'Walk' 'Run'
>> map={'a','b','Jog','Walk','e','f','Run'}
map =
'a' 'b' 'Jog' 'Walk' 'e' 'f' 'Run'
>> map(compute)
ans =
'Run' 'Walk' 'Jog'
'Jog' 'Walk' 'Run'
I assume you have the map in a form of a cell array
>> map{3} = 'Jog';
>> map{4} = 'Walk';
>> map{7} = 'Run';
Now use the map
map( compute )
will give you a cell array of strings