有没有办法让这个C开关盒更简单?

时间:2017-02-22 12:00:46

标签: c logistic-regression

我使用逻辑回归模型得到一个公式。因为我把每小时都当作一个因素,所以这个开关盒很长,不方便修改,而且不漂亮。有没有办法简化它?也许我应该尝试使用矩阵进行算术运算?

...
#define elif else if
...
// hours
switch (hours) {
    case 0:
        prob[2] = prob_base[7];
        break;
    case 1:
        prob[2] = prob_base[8];
        break;
    case 2:
        prob[2] = prob_base[9];
        break;
    case 3:
        prob[2] = prob_base[10];
        break;
    case 4:
        prob[2] = prob_base[11];
        break;
    case 5:
        prob[2] = prob_base[12];
        break;
    case 6:
        prob[2] = prob_base[13];
        break;
    case 7:
        prob[2] = prob_base[14];
        break;
    case 8:
        prob[2] = prob_base[15];
        break;
    case 9:
        prob[2] = prob_base[16];
        break;
    case 10:
        prob[2] = prob_base[17];
        break;
    case 11:
        prob[2] = prob_base[18];
        break;
    case 12:
        prob[2] = prob_base[19];
        break;
    case 13:
        prob[2] = prob_base[20];
        break;
    case 14:
        prob[2] = prob_base[21];
        break;
    case 15:
        prob[2] = prob_base[22];
        break;
    case 16:
        prob[2] = prob_base[23];
        break;
    case 17:
        prob[2] = prob_base[24];
        break;
    case 18:
        prob[2] = prob_base[25];
        break;
    case 19:
        prob[2] = prob_base[26];
        break;
    case 20:
        prob[2] = prob_base[27];
        break;
    case 21:
        prob[2] = prob_base[28];
        break;
    case 22:
        prob[2] = prob_base[29];
        break;
    case 23:
        prob[2] = prob_base[30];
        break;
    default:
        prob[2] = 0;
        break;
}

// bidf
prob[3] = prob_base[31] * atof(bidf);

// isp
switch (isp) {
    case 1:
        prob[4] = prob_base[32];
        break;
    case 2:
        prob[4] = prob_base[33];
        break;
    case 3:
        prob[4] = prob_base[34];
        break;
    default:
        prob[4] = 0;
        break;
}
...

2 个答案:

答案 0 :(得分:2)

这样的事情:

if (hours >= 0 && hours < 24)
  prob[2] = prob_base[hours + 7];
else
  prob[2] = 0;

prob[3] = prob_base[31] * atof(bidf);

if (isp >= 1 && isp < 4)
  prob[4] = prob_base[isp + 31];
else
  prob[4] = 0;

答案 1 :(得分:-1)

&reg;