Matlab获得表中与两个完全字符串匹配的行的平均值

时间:2018-02-06 18:46:30

标签: string matlab average

我在Matlab中有一个表格格式如下:

 sub     type    window     mean      stdev      rms  
      _____    ____    ______    _______    ______    ______
      'S03'    'e4'    'w1'      -0.6357    0.3274     0.715
      'S03'    'e4'    'w1'       -0.591    0.3106    0.6676
      'S03'    'e4'    'w2'      -0.4616    0.3607    0.5857
      'S03'    'e4'    'w2'       -0.457    0.3387    0.5687
      'S03'    'e4'    'w2'      -0.5737    0.2689    0.6335
      'S03'    'e4'    'w3'      -0.4818    0.2139    0.5271
      'S03'    'e4'    'w3'      -0.4272    0.1523    0.4536
      'S06'    'e4'    'w1'      -0.6357    0.3274     0.715
      'S06'    'e4'    'w2'       -0.591    0.3106    0.6676
      'S06'    'e4'    'w2'      -0.4616    0.3607    0.5857
      'S06'    'e4'    'w2'       -0.457    0.3387    0.5687
      'S06'    'e4'    'w2'      -0.5737    0.2689    0.6335
      'S06'    'e4'    'w3'      -0.4818    0.2139    0.5271

对于每个主题(例如上面显示的S03和S06),我需要获得每个窗口的平均值,stdev和rms(上面显示的3个窗口:w1-w3)。

所以我需要检查主题字符串(第1列)和窗口字符串(第3列)以检查行是否属于同一主题和窗口,然后计算平均值,stdev和rms的平均值列。我已尝试将strcmp()any()一起使用,但似乎无法使其发挥作用。

1 个答案:

答案 0 :(得分:1)

您可以将findgroupssplitapply一起使用,如下所示:

C = {
  'S03'    'e4'    'w1'      -0.6357    0.3274     0.715
  'S03'    'e4'    'w1'       -0.591    0.3106    0.6676
  'S03'    'e4'    'w2'      -0.4616    0.3607    0.5857
  'S03'    'e4'    'w2'       -0.457    0.3387    0.5687
  'S03'    'e4'    'w2'      -0.5737    0.2689    0.6335
  'S03'    'e4'    'w3'      -0.4818    0.2139    0.5271
  'S03'    'e4'    'w3'      -0.4272    0.1523    0.4536
  'S06'    'e4'    'w1'      -0.6357    0.3274     0.715
  'S06'    'e4'    'w2'       -0.591    0.3106    0.6676
  'S06'    'e4'    'w2'      -0.4616    0.3607    0.5857
  'S06'    'e4'    'w2'       -0.457    0.3387    0.5687
  'S06'    'e4'    'w2'      -0.5737    0.2689    0.6335
  'S06'    'e4'    'w3'      -0.4818    0.2139    0.5271
};
T = cell2table(C,'VariableNames',{'sub' 'type' 'window' 'mean' 'stdev' 'rms'});

[G,W,S] = findgroups(T.window,T.sub);
R_mean = splitapply(@mean,T.mean,G);
R_stdev = splitapply(@mean,T.stdev,G);
R_rms = splitapply(@mean,T.rms,G);

result = table(W,S,R_mean,R_stdev,R_rms);

输出:

 W        S            R_mean               R_stdev               R_rms      
____    _____    __________________    _________________    _________________

'w1'    'S03'              -0.61335                0.319               0.6913
'w1'    'S06'               -0.6357               0.3274                0.715
'w2'    'S03'    -0.497433333333333    0.322766666666667    0.595966666666667
'w2'    'S06'             -0.520825             0.319725             0.613875
'w3'    'S03'               -0.4545               0.1831              0.49035
'w3'    'S06'               -0.4818               0.2139               0.5271