Mia
Mia

Reputation: 181

Map matrix element to string

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 mapping as

Out=[Run, Walk, Jog; Jog, Walk, Run]

The actual output might be a cell array of strings.

Upvotes: 3

Views: 154

Answers (4)

Daniel
Daniel

Reputation: 36720

>> 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'

Upvotes: 3

chappjc
chappjc

Reputation: 30589

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'

Upvotes: 1

nova
nova

Reputation: 1

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'

Upvotes: 0

Shai
Shai

Reputation: 114876

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

Upvotes: 2

Related Questions