我有一些运行数百万次的 Matlab 代码,如以下问题所述:Matlab:从循环中重复调用相同的 mex 函数会产生太多开销吗?
我正在尝试对其进行混合以查看是否有帮助。现在,当我使用 Matlab Coder 工具从 Matlab 代码生成代码时,代码通常是合理的,但是这一行 Matlab 代码(在下面第一行的 C++ 注释中)引发了这种怪物,我不知道为什么。任何有助于理解和降低其复杂性的帮助将不胜感激。
对于上下文,d 是二维矩阵,s1 是行向量。s1_idx 在前面的 C++ 代码中被赋值为 length(s1) + 1,
/* d(:, 1) = 0:length(s1); */
cdiff = s1_idx_0 - 1;
for (nm1d2 = 0; nm1d2 <= cdiff; nm1d2++) {
tmp_data[nm1d2] = nm1d2;
}
ndbl = (int32_T)muDoubleScalarFloor((real_T)s1_sizes[1] + 0.5);
apnd = ndbl;
cdiff = ndbl - s1_sizes[1];
if (muDoubleScalarAbs((real_T)cdiff) < 4.4408920985006262E-16 * (real_T)s1_sizes[1]) {
ndbl++;
apnd = s1_sizes[1];
} else if (cdiff > 0) {
apnd = ndbl - 1;
} else {
ndbl++;
}
if (ndbl > 0) {
b_tmp_data[0] = 0.0;
if (ndbl > 1) {
b_tmp_data[ndbl - 1] = (real_T)apnd;
nm1 = ndbl - 1;
nm1d2 = nm1;
nm1d2 = (int32_T)((uint32_T)nm1d2 >> 1);
for (cdiff = 1; cdiff <= nm1d2 - 1; cdiff++) {
b_tmp_data[cdiff] = (real_T)cdiff;
b_tmp_data[(ndbl - cdiff) - 1] = (real_T)(apnd - cdiff);
}
if (nm1d2 << 1 == nm1) {
b_tmp_data[nm1d2] = (real_T)apnd / 2.0;
} else {
b_tmp_data[nm1d2] = (real_T)nm1d2;
b_tmp_data[nm1d2 + 1] = (real_T)(apnd - nm1d2);
}
}
}
cdiff = s1_idx_0 - 1;
for (nm1d2 = 0; nm1d2 <= cdiff; nm1d2++) {
SD->f0.d_data[tmp_data[nm1d2]] = b_tmp_data[nm1d2];
}