Map matrix element to string

前端 未结 4 1083
深忆病人
深忆病人 2021-01-19 13:02

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

相关标签:
4条回答
  • 2021-01-19 13:22

    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'
    
    0 讨论(0)
  • 2021-01-19 13:29

    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'
    
    0 讨论(0)
  • 2021-01-19 13:30
    >> 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'
    
    0 讨论(0)
  • 2021-01-19 13:42

    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

    0 讨论(0)
提交回复
热议问题