Map matrix element to string

前端 未结 4 1192
深忆病人
深忆病人 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'
    

提交回复
热议问题