MATLAB:使用双数组中的值来确定字符串元胞数组中的值

MATLAB: Use values in double array to determine values in a cell array of strings

我有一个 597x4 的双精度数组,它构成了某个科目的学生分数。该数组中的每个值都在 0 到 100 之间。

我想创建一个 597x4 的字符串元胞数组,其中每个元胞包含一个字符串,该字符串连接到双精度数组中的等效位置。

每个得分在 80 分或以上的人都会获得 'A'。因此,如果双精度数组的 (1,1) 包含 84,则元胞数组的 (1,1) 应为 'A'。假设细分为 > 80 = 'A'、< 80 但 > 50 = 'B',否则为 'C'.

我意识到这可以通过循环轻松完成,但是有更优雅的方法吗?这是在 MATLAB 中,但最终我想将值粘贴到 Excel 文件中。

这是一个 bsxfun 和一些重塑的 -

factions = [0,30,50,80] %// Edit this if you want more factions
remarks = {'Doomed','Fail','Good','Very good'}; %// Editable too

rank = sum(bsxfun(@ge,arr,permute(factions,[1 3 2])),3);
out = reshape(cellstr(char(max(rank(:))-rank(:)+'A')),size(arr))
out_remarks = remarks(rank)

样本输入、输出-

arr =
     3    81    73    38    88
    56    90    58    94    94
    31    60     3    83    67
    94    89    45    85    21
    99    95    65    38    66
    29    55    53    60     8
factions =
     0    30    50    80
out = 
    'D'    'A'    'B'    'C'    'A'
    'B'    'A'    'B'    'A'    'A'
    'C'    'B'    'D'    'A'    'B'
    'A'    'A'    'C'    'A'    'D'
    'A'    'A'    'B'    'C'    'B'
    'D'    'B'    'B'    'B'    'D'
out_remarks = 
    'Doomed'       'Very good'    'Good'      'Fail'         'Very good'
    'Good'         'Very good'    'Good'      'Very good'    'Very good'
    'Fail'         'Good'         'Doomed'    'Very good'    'Good'     
    'Very good'    'Very good'    'Fail'      'Very good'    'Doomed'   
    'Very good'    'Very good'    'Good'      'Fail'         'Good'     
    'Doomed'       'Good'         'Good'      'Good'         'Doomed'