Ejemplo n.º 1
0
/*
 * Arguments    : emxArray_real_T *A
 *                const emxArray_real_T *B
 * Return Type  : void
 */
void b_mrdivide(emxArray_real_T *A, const emxArray_real_T *B)
{
  emxArray_real_T *b_B;
  emxArray_real_T *b_A;
  int i8;
  int loop_ub;
  double d2;
  emxInit_real_T1(&b_B, 1);
  emxInit_real_T1(&b_A, 1);
  if ((A->size[1] == 0) || (B->size[1] == 0)) {
    i8 = A->size[0] * A->size[1];
    A->size[0] = 1;
    A->size[1] = 1;
    emxEnsureCapacity((emxArray__common *)A, i8, (int)sizeof(double));
    A->data[0] = 0.0;
  } else if (1 == B->size[1]) {
    if (A->size[1] == 0) {
    } else {
      A->data[0] *= 1.0 / B->data[0];
    }
  } else {
    i8 = b_B->size[0];
    b_B->size[0] = B->size[1];
    emxEnsureCapacity((emxArray__common *)b_B, i8, (int)sizeof(double));
    loop_ub = B->size[1];
    for (i8 = 0; i8 < loop_ub; i8++) {
      b_B->data[i8] = B->data[B->size[0] * i8];
    }

    i8 = b_A->size[0];
    b_A->size[0] = A->size[1];
    emxEnsureCapacity((emxArray__common *)b_A, i8, (int)sizeof(double));
    loop_ub = A->size[1];
    for (i8 = 0; i8 < loop_ub; i8++) {
      b_A->data[i8] = A->data[A->size[0] * i8];
    }

    d2 = qrsolve(b_B, b_A);
    i8 = A->size[0] * A->size[1];
    A->size[0] = 1;
    A->size[1] = 1;
    emxEnsureCapacity((emxArray__common *)A, i8, (int)sizeof(double));
    A->data[0] = d2;
  }

  emxFree_real_T(&b_A);
  emxFree_real_T(&b_B);
}
void G_api(const mxArray * const prhs[3], const mxArray *plhs[1])
{
  emxArray_real_T *Y0;
  emxArray_real_T *vecS0;
  real_T (*para)[2];
  real_T g;
  emlrtStack st = { NULL, NULL, NULL };

  st.tls = emlrtRootTLSGlobal;
  emlrtHeapReferenceStackEnterFcnR2012b(&st);
  emxInit_real_T(&st, &Y0, 2, &l_emlrtRTEI, true);
  emxInit_real_T1(&st, &vecS0, 1, &l_emlrtRTEI, true);

  /* Marshall function inputs */
  para = emlrt_marshallIn(&st, emlrtAlias((const mxArray *)prhs[0]), "para");
  c_emlrt_marshallIn(&st, emlrtAlias((const mxArray *)prhs[1]), "Y0", Y0);
  e_emlrt_marshallIn(&st, emlrtAlias((const mxArray *)prhs[2]), "vecS0", vecS0);

  /* Invoke the target function */
  g = G(&st, *para, Y0, vecS0);

  /* Marshall function outputs */
  plhs[0] = emlrt_marshallOut(g);
  vecS0->canFreeData = false;
  emxFree_real_T(&vecS0);
  Y0->canFreeData = false;
  emxFree_real_T(&Y0);
  emlrtHeapReferenceStackLeaveFcnR2012b(&st);
}
void makeHistFutTablewithRearrange_api(const mxArray * const prhs[4], const
  mxArray *plhs[1])
{
  emxArray_uint8_T *testm;
  emxArray_real_T *rmat;
  emxArray_real_T *y;
  real_T range;
  real_T dims;
  emlrtStack st = { NULL, NULL, NULL };

  st.tls = emlrtRootTLSGlobal;
  emlrtHeapReferenceStackEnterFcnR2012b(&st);
  emxInit_uint8_T(&st, &testm, 2, &h_emlrtRTEI, true);
  emxInit_real_T1(&st, &rmat, 1, &h_emlrtRTEI, true);
  emxInit_real_T(&st, &y, 2, &h_emlrtRTEI, true);

  /* Marshall function inputs */
  emlrt_marshallIn(&st, emlrtAlias((const mxArray *)prhs[0]), "testm", testm);
  range = c_emlrt_marshallIn(&st, emlrtAliasP((const mxArray *)prhs[1]), "range");
  dims = c_emlrt_marshallIn(&st, emlrtAliasP((const mxArray *)prhs[2]), "dims");
  e_emlrt_marshallIn(&st, emlrtAlias((const mxArray *)prhs[3]), "rmat", rmat);

  /* Invoke the target function */
  makeHistFutTablewithRearrange(&st, testm, range, dims, rmat, y);

  /* Marshall function outputs */
  plhs[0] = emlrt_marshallOut(y);
  y->canFreeData = false;
  emxFree_real_T(&y);
  rmat->canFreeData = false;
  emxFree_real_T(&rmat);
  testm->canFreeData = false;
  emxFree_uint8_T(&testm);
  emlrtHeapReferenceStackLeaveFcnR2012b(&st);
}
Ejemplo n.º 4
0
/*
 * Arguments    : int numDimensions
 *                int *size
 * Return Type  : emxArray_real_T *
 */
emxArray_real_T *emxCreateND_real_T(int numDimensions, int *size)
{
  emxArray_real_T *emx;
  int numEl;
  int i;
  emxInit_real_T1(&emx, numDimensions);
  numEl = 1;
  for (i = 0; i < numDimensions; i++) {
    numEl *= size[i];
    emx->size[i] = size[i];
  }

  emx->data = (double *)calloc((unsigned int)numEl, sizeof(double));
  emx->numDimensions = numDimensions;
  emx->allocatedSize = numEl;
  return emx;
}
Ejemplo n.º 5
0
/*
 * Arguments    : double *data
 *                int numDimensions
 *                int *size
 * Return Type  : emxArray_real_T *
 */
emxArray_real_T *emxCreateWrapperND_real_T(double *data, int numDimensions, int *
  size)
{
  emxArray_real_T *emx;
  int numEl;
  int i;
  emxInit_real_T1(&emx, numDimensions);
  numEl = 1;
  for (i = 0; i < numDimensions; i++) {
    numEl *= size[i];
    emx->size[i] = size[i];
  }

  emx->data = data;
  emx->numDimensions = numDimensions;
  emx->allocatedSize = numEl;
  emx->canFreeData = false;
  return emx;
}
Ejemplo n.º 6
0
/*
 * Arguments    : int rows
 *                int cols
 * Return Type  : emxArray_real_T *
 */
emxArray_real_T *emxCreate_real_T(int rows, int cols)
{
  emxArray_real_T *emx;
  int size[2];
  int numEl;
  int i;
  size[0] = rows;
  size[1] = cols;
  emxInit_real_T1(&emx, 2);
  numEl = 1;
  for (i = 0; i < 2; i++) {
    numEl *= size[i];
    emx->size[i] = size[i];
  }

  emx->data = (double *)calloc((unsigned int)numEl, sizeof(double));
  emx->numDimensions = 2;
  emx->allocatedSize = numEl;
  return emx;
}
Ejemplo n.º 7
0
/*
 * Arguments    : double *data
 *                int rows
 *                int cols
 * Return Type  : emxArray_real_T *
 */
emxArray_real_T *emxCreateWrapper_real_T(double *data, int rows, int cols)
{
  emxArray_real_T *emx;
  int size[2];
  int numEl;
  int i;
  size[0] = rows;
  size[1] = cols;
  emxInit_real_T1(&emx, 2);
  numEl = 1;
  for (i = 0; i < 2; i++) {
    numEl *= size[i];
    emx->size[i] = size[i];
  }

  emx->data = data;
  emx->numDimensions = 2;
  emx->allocatedSize = numEl;
  emx->canFreeData = false;
  return emx;
}
Ejemplo n.º 8
0
/*
 * Arguments    : emxArray_real_T **pEmxArray
 *                int numDimensions
 * Return Type  : void
 */
void emxInitArray_real_T(emxArray_real_T **pEmxArray, int numDimensions)
{
  emxInit_real_T1(pEmxArray, numDimensions);
}
void occflow_api(const mxArray *prhs[18], const mxArray *plhs[4])
{
  emxArray_real_T *cgridvec;
  emxArray_real_T *cgridvecprev;
  emxArray_real_T *context;
  emxArray_real_T *nei_idx;
  emxArray_real_T *nei_weight;
  emxArray_real_T *nei4u_idx;
  emxArray_real_T *nei4u_weight;
  emxArray_real_T *predvec;
  emxArray_real_T *maxvec;
  real_T nei_filter_n;
  real_T nei4u_filter_n;
  real_T occval;
  real_T minthreshold;
  real_T maxthreshold;
  real_T reinitval;
  real_T intensifyrate;
  real_T nocc_attenuaterate;
  real_T unknown_attenuaterate;
  real_T sigm_coef;
  real_T do_attenuation_first;
  emlrtStack st = { NULL, NULL, NULL };

  st.tls = emlrtRootTLSGlobal;
  emlrtHeapReferenceStackEnterFcnR2012b(&st);
  emxInit_real_T(&st, &cgridvec, 1, &b_emlrtRTEI, true);
  emxInit_real_T(&st, &cgridvecprev, 1, &b_emlrtRTEI, true);
  emxInit_real_T1(&st, &context, 2, &b_emlrtRTEI, true);
  emxInit_real_T1(&st, &nei_idx, 2, &b_emlrtRTEI, true);
  emxInit_real_T1(&st, &nei_weight, 2, &b_emlrtRTEI, true);
  emxInit_real_T1(&st, &nei4u_idx, 2, &b_emlrtRTEI, true);
  emxInit_real_T1(&st, &nei4u_weight, 2, &b_emlrtRTEI, true);
  emxInit_real_T(&st, &predvec, 1, &b_emlrtRTEI, true);
  emxInit_real_T(&st, &maxvec, 1, &b_emlrtRTEI, true);
  prhs[1] = emlrtProtectR2012b(prhs[1], 1, true, -1);
  prhs[2] = emlrtProtectR2012b(prhs[2], 2, true, -1);

  /* Marshall function inputs */
  emlrt_marshallIn(&st, emlrtAlias(prhs[0]), "cgridvec", cgridvec);
  emlrt_marshallIn(&st, emlrtAlias(prhs[1]), "cgridvecprev", cgridvecprev);
  c_emlrt_marshallIn(&st, emlrtAlias(prhs[2]), "context", context);
  c_emlrt_marshallIn(&st, emlrtAlias(prhs[3]), "nei_idx", nei_idx);
  c_emlrt_marshallIn(&st, emlrtAlias(prhs[4]), "nei_weight", nei_weight);
  nei_filter_n = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[5]), "nei_filter_n");
  c_emlrt_marshallIn(&st, emlrtAlias(prhs[6]), "nei4u_idx", nei4u_idx);
  c_emlrt_marshallIn(&st, emlrtAlias(prhs[7]), "nei4u_weight", nei4u_weight);
  nei4u_filter_n = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[8]),
    "nei4u_filter_n");
  occval = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[9]), "occval");
  minthreshold = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[10]), "minthreshold");
  maxthreshold = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[11]), "maxthreshold");
  reinitval = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[12]), "reinitval");
  intensifyrate = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[13]), "intensifyrate");
  nocc_attenuaterate = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[14]),
    "nocc_attenuaterate");
  unknown_attenuaterate = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[15]),
    "unknown_attenuaterate");
  sigm_coef = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[16]), "sigm_coef");
  do_attenuation_first = e_emlrt_marshallIn(&st, emlrtAliasP(prhs[17]),
    "do_attenuation_first");

  /* Invoke the target function */
  occflow(&st, cgridvec, cgridvecprev, context, nei_idx, nei_weight,
          nei_filter_n, nei4u_idx, nei4u_weight, nei4u_filter_n, occval,
          minthreshold, maxthreshold, reinitval, intensifyrate,
          nocc_attenuaterate, unknown_attenuaterate, sigm_coef,
          do_attenuation_first, predvec, maxvec);

  /* Marshall function outputs */
  plhs[0] = c_emlrt_marshallOut(predvec);
  d_emlrt_marshallOut(cgridvecprev, prhs[1]);
  plhs[1] = prhs[1];
  e_emlrt_marshallOut(context, prhs[2]);
  plhs[2] = prhs[2];
  plhs[3] = c_emlrt_marshallOut(maxvec);
  maxvec->canFreeData = false;
  emxFree_real_T(&maxvec);
  predvec->canFreeData = false;
  emxFree_real_T(&predvec);
  nei4u_weight->canFreeData = false;
  emxFree_real_T(&nei4u_weight);
  nei4u_idx->canFreeData = false;
  emxFree_real_T(&nei4u_idx);
  nei_weight->canFreeData = false;
  emxFree_real_T(&nei_weight);
  nei_idx->canFreeData = false;
  emxFree_real_T(&nei_idx);
  context->canFreeData = false;
  emxFree_real_T(&context);
  cgridvecprev->canFreeData = false;
  emxFree_real_T(&cgridvecprev);
  cgridvec->canFreeData = false;
  emxFree_real_T(&cgridvec);
  emlrtHeapReferenceStackLeaveFcnR2012b(&st);
}
Ejemplo n.º 10
0
/*
 * 计算肯德尔系数
 * data1;data2均为一列数据
 * Arguments    : const emxArray_real_T *data1
 *                const emxArray_real_T *data2
 *                double limit_min1
 *                double limit_min2
 * Return Type  : double
 */
double Kendall(const emxArray_real_T *data1, const emxArray_real_T *data2,
               double limit_min1, double limit_min2)
{
  double R;
  int row;
  emxArray_real_T *sample1;
  int i2;
  int loop_ub;
  unsigned int mm;
  int ii;
  emxArray_real_T *sample;
  int m;
  emxArray_int32_T *b_ii;
  emxArray_int32_T *iwork;
  int i;
  int j;
  int pEnd;
  int p;
  int q;
  int qEnd;
  int kEnd;
  emxArray_real_T *nozero;
  unsigned int sample_idx_0;
  double P;
  int aa;
  int bb;
  if (data1->size[0] <= data2->size[0]) {
    row = data1->size[0];
  } else {
    row = data2->size[0];
  }

  emxInit_real_T(&sample1, 2);
  i2 = sample1->size[0] * sample1->size[1];
  sample1->size[0] = row;
  sample1->size[1] = 2;
  emxEnsureCapacity((emxArray__common *)sample1, i2, (int)sizeof(double));
  loop_ub = row << 1;
  for (i2 = 0; i2 < loop_ub; i2++) {
    sample1->data[i2] = 0.0;
  }

  mm = 0U;
  for (ii = 0; ii < row; ii++) {
    if ((data1->data[ii] < limit_min1) || (data2->data[ii] < limit_min2) ||
        rtIsNaN(data1->data[ii]) || rtIsNaN(data2->data[ii])) {
    } else {
      mm++;
      sample1->data[(int)mm - 1] = data1->data[ii];
      sample1->data[((int)mm + sample1->size[0]) - 1] = data2->data[ii];
    }
  }

  if (1 > (int)mm) {
    loop_ub = 0;
  } else {
    loop_ub = (int)mm;
  }

  emxInit_real_T(&sample, 2);

  /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
  /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
  /* 计算Kendall(肯德尔)秩相关系数,用于二维最大熵函数的拟合 */
  i2 = sample->size[0] * sample->size[1];
  sample->size[0] = loop_ub;
  sample->size[1] = 2;
  emxEnsureCapacity((emxArray__common *)sample, i2, (int)sizeof(double));
  for (i2 = 0; i2 < 2; i2++) {
    for (m = 0; m < loop_ub; m++) {
      sample->data[m + sample->size[0] * i2] = sample1->data[m + sample1->size[0]
        * i2];
    }
  }

  emxFree_real_T(&sample1);
  emxInit_int32_T(&b_ii, 1);
  i2 = b_ii->size[0];
  b_ii->size[0] = loop_ub;
  emxEnsureCapacity((emxArray__common *)b_ii, i2, (int)sizeof(int));
  for (i2 = 0; i2 < loop_ub; i2++) {
    b_ii->data[i2] = 0;
  }

  if (loop_ub == 0) {
  } else {
    emxInit_int32_T(&iwork, 1);
    i2 = iwork->size[0];
    iwork->size[0] = loop_ub;
    emxEnsureCapacity((emxArray__common *)iwork, i2, (int)sizeof(int));
    for (m = 1; m <= loop_ub - 1; m += 2) {
      if (sortLE(sample, m, m + 1)) {
        b_ii->data[m - 1] = m;
        b_ii->data[m] = m + 1;
      } else {
        b_ii->data[m - 1] = m + 1;
        b_ii->data[m] = m;
      }
    }

    if ((loop_ub & 1) != 0) {
      b_ii->data[loop_ub - 1] = loop_ub;
    }

    i = 2;
    while (i < loop_ub) {
      i2 = i << 1;
      j = 1;
      for (pEnd = 1 + i; pEnd < loop_ub + 1; pEnd = qEnd + i) {
        p = j;
        q = pEnd;
        qEnd = j + i2;
        if (qEnd > loop_ub + 1) {
          qEnd = loop_ub + 1;
        }

        m = 0;
        kEnd = qEnd - j;
        while (m + 1 <= kEnd) {
          if (sortLE(sample, b_ii->data[p - 1], b_ii->data[q - 1])) {
            iwork->data[m] = b_ii->data[p - 1];
            p++;
            if (p == pEnd) {
              while (q < qEnd) {
                m++;
                iwork->data[m] = b_ii->data[q - 1];
                q++;
              }
            }
          } else {
            iwork->data[m] = b_ii->data[q - 1];
            q++;
            if (q == qEnd) {
              while (p < pEnd) {
                m++;
                iwork->data[m] = b_ii->data[p - 1];
                p++;
              }
            }
          }

          m++;
        }

        for (m = 0; m + 1 <= kEnd; m++) {
          b_ii->data[(j + m) - 1] = iwork->data[m];
        }

        j = qEnd;
      }

      i = i2;
    }

    emxFree_int32_T(&iwork);
  }

  emxInit_real_T1(&nozero, 1);
  m = sample->size[0];
  sample_idx_0 = (unsigned int)sample->size[0];
  i2 = nozero->size[0];
  nozero->size[0] = (int)sample_idx_0;
  emxEnsureCapacity((emxArray__common *)nozero, i2, (int)sizeof(double));
  for (j = 0; j < 2; j++) {
    for (i = 0; i + 1 <= m; i++) {
      nozero->data[i] = sample->data[(b_ii->data[i] + sample->size[0] * j) - 1];
    }

    for (i = 0; i + 1 <= m; i++) {
      sample->data[i + sample->size[0] * j] = nozero->data[i];
    }
  }

  emxFree_int32_T(&b_ii);
  emxFree_real_T(&nozero);

  /* 按波高为关键值,对波高和周期的组合进行排序 */
  P = 0.0;
  for (aa = 0; aa < loop_ub; aa++) {
    i2 = loop_ub + (int)(1.0 - ((1.0 + (double)aa) + 1.0));
    for (bb = 0; bb < i2; bb++) {
      if (sample->data[aa + sample->size[0]] <= sample->data[((int)((unsigned
             int)aa + bb) + sample->size[0]) + 1]) {
        P++;
      }
    }
  }

  emxFree_real_T(&sample);
  R = 4.0 * P / ((double)loop_ub * ((double)loop_ub - 1.0)) - 1.0;

  /* Kendall(肯德尔)秩相关系数 */
  return R;
}
// 
// % Erased all parts not wanted for codegeneration /Martin %% 
// Arguments    : emxArray_real_T *s 
//                emxArray_boolean_T *outliers 
//                emxArray_boolean_T *outliers2 
// Return Type  : void 
//
void locateOutliers(emxArray_real_T *s, emxArray_boolean_T *outliers, emxArray_boolean_T *outliers2)
{
    int ia;
    int ic;
    emxArray_real_T *b_y1;
    int orderForDim;
    int iyLead;
    double work_data_idx_0;
    int m;
    double tmp1;
    double tmp2;
    emxArray_real_T *SigmaMat;
    emxArray_int32_T *r0;
    emxArray_int32_T *r1;
    emxArray_real_T *a;
    unsigned int s_idx_0;
    emxArray_real_T *C;
    int br;
    double mu;
    emxArray_real_T *b_s;
    /* locateOutliers: locates artifacts/outliers from data series */
    /*  */
    /*   Inputs:  s = array containg data series */
    /*            method = artifact removal method to use. */
    /*   methods: 'percent' = percentage filter: locates data > x percent diff than previous data point.                */
    /*            'sd' = standard deviation filter: locates data > x stdev away from mean. */
    /*            'above' = Threshold filter: locates data > threshold value */
    /*            'below' = Threshold filter: locates data < threshold value */
    /*            'median' = median filter. Outliers are located. */
    /* Outputs:   outliers = logical array of whether s is artifact/outlier or not */
    /*                        eg. - [0 0 0 1 0], 1=artifact, 0=normal */
    /*                         */
    /* Examples: */
    /*    Locate outliers with 20% percentage filter: */
    /*        outliers = locateOutlers(s,'percent',0.2) */
    /*    Locate outliers that are above a threshold of 0.5: */
    /*        outliers = locateOutlers(s,'thresh','above',0.5) */
    /*    Locate outliers with median filter: */
    /*        outliers = locateOutlers(s,'median',4,5) */
    /*  */
    /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
    /*  Copyright (C) 2010, John T. Ramshur, [email protected] */
    /*   */
    /*  This file is part of HRVAS */
    /*  */
    /*  HRVAS is free software: you can redistribute it and/or modify */
    /*  it under the terms of the GNU General Public License as published by */
    /*  the Free Software Foundation, either version 3 of the License, or */
    /*  (at your option) any later version. */
    /*   */
    /*  HRVAS is distributed in the hope that it will be useful, */
    /*  but WITHOUT ANY WARRANTY; without even the implied warranty of */
    /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
    /*  GNU General Public License for more details. */
    /*   */
    /*  You should have received a copy of the GNU General Public License */
    /*  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. */
    /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
    ia = outliers->size[0];
    outliers->size[0] = s->size[0];
    emxEnsureCapacity((emxArray__common *)outliers, ia, (int)sizeof(boolean_T));
    ic = s->size[0];
    for (ia = 0; ia < ic; ia++) {
        outliers->data[ia] = false;
    }
    /* preallocate         */
    if (1 > s->size[0] - 1) {
        ic = 0;
    } else {
        ic = s->size[0] - 1;
    }
    emxInit_real_T(&b_y1, 1);
    if (s->size[0] == 0) {
        ia = b_y1->size[0];
        b_y1->size[0] = 0;
        emxEnsureCapacity((emxArray__common *)b_y1, ia, (int)sizeof(double));
    } else {
        if (s->size[0] - 1 <= 1) {
            orderForDim = s->size[0] - 1;
        } else {
            orderForDim = 1;
        }
        if (orderForDim < 1) {
            ia = b_y1->size[0];
            b_y1->size[0] = 0;
            emxEnsureCapacity((emxArray__common *)b_y1, ia, (int)sizeof(double));
        } else {
            orderForDim = s->size[0] - 1;
            ia = b_y1->size[0];
            b_y1->size[0] = orderForDim;
            emxEnsureCapacity((emxArray__common *)b_y1, ia, (int)sizeof(double));
            if (!(b_y1->size[0] == 0)) {
                orderForDim = 1;
                iyLead = 0;
                work_data_idx_0 = s->data[0];
                for (m = 2; m <= s->size[0]; m++) {
                    tmp1 = s->data[orderForDim];
                    tmp2 = work_data_idx_0;
                    work_data_idx_0 = tmp1;
                    tmp1 -= tmp2;
                    orderForDim++;
                    b_y1->data[iyLead] = tmp1;
                    iyLead++;
                }
            }
        }
    }
    emxInit_real_T(&SigmaMat, 1);
    b_abs(b_y1, SigmaMat);
    /* percent chage from previous */
    /* find index of values where pChange > perLimit */
    orderForDim = s->size[0];
    if (2 > orderForDim) {
        ia = 0;
        orderForDim = 0;
    } else {
        ia = 1;
        orderForDim = s->size[0];
    }
    emxInit_int32_T(&r0, 2);
    iyLead = r0->size[0] * r0->size[1];
    r0->size[0] = 1;
    r0->size[1] = orderForDim - ia;
    emxEnsureCapacity((emxArray__common *)r0, iyLead, (int)sizeof(int));
    orderForDim -= ia;
    for (iyLead = 0; iyLead < orderForDim; iyLead++) {
        r0->data[r0->size[0] * iyLead] = ia + iyLead;
    }
    emxInit_int32_T1(&r1, 1);
    ia = r1->size[0];
    r1->size[0] = ic;
    emxEnsureCapacity((emxArray__common *)r1, ia, (int)sizeof(int));
    for (ia = 0; ia < ic; ia++) {
        r1->data[ia] = 1 + ia;
    }
    ic = r0->size[0] * r0->size[1];
    for (ia = 0; ia < ic; ia++) {
        outliers->data[r0->data[ia]] = (SigmaMat->data[ia] / s->data[r1->data[ia] - 1] > 0.2);
    }
    emxFree_int32_T(&r1);
    emxFree_int32_T(&r0);
    emxInit_real_T1(&a, 2);
    /*  Reference:  */
    /*  Clifford, G. (2002). "Characterizing Artefact in the Normal  */
    /*  Human 24-Hour RR Time Series to Aid Identification and Artificial  */
    /*  Replication of Circadian Variations in Human Beat to Beat Heart Rate  */
    /*  using a Simple Threshold." */
    /*  */
    /*  Aubert, A. E., D. Ramaekers, et al. (1999). "The analysis of heart  */
    /*  rate variability in unrestrained rats. Validation of method and  */
    /*  results." Comput Methods Programs Biomed 60(3): 197-213. */
    /* convert to logical array */
    /* preallocate   */
    iyLead = s->size[0];
    s_idx_0 = (unsigned int)s->size[0];
    ia = a->size[0] * a->size[1];
    a->size[0] = (int)s_idx_0;
    a->size[1] = 2;
    emxEnsureCapacity((emxArray__common *)a, ia, (int)sizeof(double));
    for (orderForDim = 1; orderForDim <= iyLead; orderForDim++) {
        a->data[orderForDim - 1] = (double)orderForDim / (double)iyLead;
        a->data[(orderForDim + a->size[0]) - 1] = 1.0;
    }
    mldivide(a, s, b_y1);
    emxInit_real_T(&C, 1);
    if (b_y1->size[0] == 1) {
        ia = C->size[0];
        C->size[0] = a->size[0];
        emxEnsureCapacity((emxArray__common *)C, ia, (int)sizeof(double));
        ic = a->size[0];
        for (ia = 0; ia < ic; ia++) {
            C->data[ia] = 0.0;
            for (iyLead = 0; iyLead < 2; iyLead++) {
                C->data[ia] += a->data[ia + a->size[0] * iyLead] * b_y1->data[iyLead];
            }
        }
    } else {
        s_idx_0 = (unsigned int)a->size[0];
        ia = C->size[0];
        C->size[0] = (int)s_idx_0;
        emxEnsureCapacity((emxArray__common *)C, ia, (int)sizeof(double));
        m = a->size[0];
        orderForDim = C->size[0];
        ia = C->size[0];
        C->size[0] = orderForDim;
        emxEnsureCapacity((emxArray__common *)C, ia, (int)sizeof(double));
        for (ia = 0; ia < orderForDim; ia++) {
            C->data[ia] = 0.0;
        }
        if (a->size[0] == 0) {
        } else {
            orderForDim = 0;
            while ((m > 0) && (orderForDim <= 0)) {
                for (ic = 1; ic <= m; ic++) {
                    C->data[ic - 1] = 0.0;
                }
                orderForDim = m;
            }
            br = 0;
            orderForDim = 0;
            while ((m > 0) && (orderForDim <= 0)) {
                orderForDim = 0;
                for (iyLead = br; iyLead + 1 <= br + 2; iyLead++) {
                    if (b_y1->data[iyLead] != 0.0) {
                        ia = orderForDim;
                        for (ic = 0; ic + 1 <= m; ic++) {
                            ia++;
                            C->data[ic] += b_y1->data[iyLead] * a->data[ia - 1];
                        }
                    }
                    orderForDim += m;
                }
                br += 2;
                orderForDim = m;
            }
        }
    }
    emxFree_real_T(&a);
    ia = s->size[0];
    emxEnsureCapacity((emxArray__common *)s, ia, (int)sizeof(double));
    ic = s->size[0];
    for (ia = 0; ia < ic; ia++) {
        s->data[ia] -= C->data[ia];
    }
    emxFree_real_T(&C);
    mu = mean(s);
    /* mean */
    orderForDim = s->size[0];
    if (s->size[0] > 1) {
        iyLead = s->size[0] - 1;
    } else {
        iyLead = s->size[0];
    }
    if (s->size[0] == 0) {
        tmp2 = 0.0;
    } else {
        ia = 0;
        work_data_idx_0 = s->data[0];
        for (ic = 2; ic <= orderForDim; ic++) {
            ia++;
            work_data_idx_0 += s->data[ia];
        }
        work_data_idx_0 /= (double)s->size[0];
        ia = 0;
        tmp1 = s->data[0] - work_data_idx_0;
        tmp2 = tmp1 * tmp1;
        for (ic = 2; ic <= orderForDim; ic++) {
            ia++;
            tmp1 = s->data[ia] - work_data_idx_0;
            tmp2 += tmp1 * tmp1;
        }
        tmp2 /= (double)iyLead;
    }
    emxInit_real_T(&b_s, 1);
    /* standard deviation */
    /*  Create a matrix of mean values by replicating the mu vector for n rows */
    repmat(mu, (double)s->size[0], b_y1);
    /*  Create a matrix of standard deviation values by replicating the sigma vector for n rows */
    repmat(std::sqrt(tmp2), (double)s->size[0], SigmaMat);
    /*  Create a matrix of zeros and ones, where ones indicate the location of outliers */
    ia = b_s->size[0];
    b_s->size[0] = s->size[0];
    emxEnsureCapacity((emxArray__common *)b_s, ia, (int)sizeof(double));
    ic = s->size[0];
    for (ia = 0; ia < ic; ia++) {
        b_s->data[ia] = s->data[ia] - b_y1->data[ia];
    }
    b_abs(b_s, b_y1);
    ia = outliers2->size[0];
    outliers2->size[0] = b_y1->size[0];
    emxEnsureCapacity((emxArray__common *)outliers2, ia, (int)sizeof(boolean_T));
    ic = b_y1->size[0];
    emxFree_real_T(&b_s);
    for (ia = 0; ia < ic; ia++) {
        outliers2->data[ia] = (b_y1->data[ia] > 3.0 * SigmaMat->data[ia]);
    }
    emxFree_real_T(&b_y1);
    emxFree_real_T(&SigmaMat);
    /*  Reference:  */
    /*  Aubert, A. E., D. Ramaekers, et al. (1999). "The analysis of heart  */
    /*  rate variability in unrestrained rats. Validation of method and  */
    /*  results." Comput Methods Programs Biomed 60(3): 197-213. */
    /* convert to logical array */
}
Ejemplo n.º 12
0
/* Function Definitions */
void occflow(const emlrtStack *sp, const emxArray_real_T *cgridvec,
             emxArray_real_T *cgridvecprev, emxArray_real_T *context, const
             emxArray_real_T *nei_idx, const emxArray_real_T *nei_weight, real_T
             nei_filter_n, const emxArray_real_T *nei4u_idx, const
             emxArray_real_T *nei4u_weight, real_T nei4u_filter_n, real_T occval,
             real_T minthreshold, real_T maxthreshold, real_T reinitval, real_T
             intensifyrate, real_T nocc_attenuaterate, real_T
             unknown_attenuaterate, real_T sigm_coef, real_T
             do_attenuation_first, emxArray_real_T *predvec, emxArray_real_T
             *maxvec)
{
  emxArray_boolean_T *x;
  int32_T ix;
  int32_T idx;
  emxArray_boolean_T *r0;
  int32_T nx;
  emxArray_int32_T *ii;
  boolean_T overflow;
  int32_T iy;
  boolean_T exitg6;
  boolean_T guard3 = false;
  boolean_T guard4 = false;
  emxArray_real_T *newlyoccidx;
  boolean_T exitg5;
  boolean_T guard2 = false;
  boolean_T b_guard3 = false;
  emxArray_real_T *occidx;
  boolean_T exitg4;
  boolean_T guard1 = false;
  boolean_T b_guard2 = false;
  emxArray_real_T *noccidx;
  int32_T nrnocc;
  int32_T j;
  emxArray_real_T *curr_col;
  emxArray_real_T *updt_col;
  emxArray_real_T *z;
  int32_T coccidx;
  boolean_T b_guard1 = false;
  int32_T ixstart;
  int32_T n;
  real_T mtmp;
  boolean_T exitg3;
  int32_T varargin_1[2];
  int32_T k;
  int32_T iv3[2];
  int32_T iv4[2];
  real_T d0;
  emxArray_real_T *tempcontext;
  emxArray_real_T *b_nei4u_weight;
  real_T sumval;
  int32_T m;
  int32_T iv5[2];
  boolean_T b_ix;
  boolean_T exitg2;
  boolean_T b_ixstart;
  int32_T varargin_2[2];
  boolean_T p;
  boolean_T exitg1;
  emlrtStack st;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  (void)unknown_attenuaterate;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  e_st.prev = &d_st;
  e_st.tls = d_st.tls;
  f_st.prev = &e_st;
  f_st.tls = e_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b(sp);
  emxInit_boolean_T(sp, &x, 1, &emlrtRTEI, true);

  /*  */
  /*  Occupancy flow with vector input  */
  /*  */
  /*  Compute indices first  */
  ix = x->size[0];
  x->size[0] = cgridvec->size[0];
  emxEnsureCapacity(sp, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (cgridvec->data[ix] == occval);
  }

  emxInit_boolean_T(sp, &r0, 1, &emlrtRTEI, true);
  ix = r0->size[0];
  r0->size[0] = cgridvecprev->size[0];
  emxEnsureCapacity(sp, (emxArray__common *)r0, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvecprev->size[0];
  for (ix = 0; ix < idx; ix++) {
    r0->data[ix] = (cgridvecprev->data[ix] != occval);
  }

  ix = x->size[0];
  nx = r0->size[0];
  if (ix != nx) {
    emlrtSizeEqCheck1DR2012b(ix, nx, &emlrtECI, sp);
  }

  st.site = &emlrtRSI;
  ix = x->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = x->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (x->data[ix] && r0->data[ix]);
  }

  emxFree_boolean_T(&r0);
  emxInit_int32_T(&st, &ii, 1, &l_emlrtRTEI, true);
  b_st.site = &i_emlrtRSI;
  nx = x->size[0];
  idx = 0;
  ix = ii->size[0];
  ii->size[0] = x->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                    &emlrtRTEI);
  c_st.site = &j_emlrtRSI;
  if (1 > x->size[0]) {
    overflow = false;
  } else {
    overflow = (x->size[0] > 2147483646);
  }

  if (overflow) {
    d_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&d_st);
  }

  iy = 1;
  exitg6 = false;
  while ((!exitg6) && (iy <= nx)) {
    guard3 = false;
    if (x->data[iy - 1]) {
      idx++;
      ii->data[idx - 1] = iy;
      if (idx >= nx) {
        exitg6 = true;
      } else {
        guard3 = true;
      }
    } else {
      guard3 = true;
    }

    if (guard3) {
      iy++;
    }
  }

  if (idx <= x->size[0]) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &s_emlrtRTEI,
      "Coder:builtins:AssertionFailed", 0);
  }

  if (x->size[0] == 1) {
    if (idx == 0) {
      ix = ii->size[0];
      ii->size[0] = 0;
      emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof
                        (int32_T), &emlrtRTEI);
    }
  } else {
    if (1 > idx) {
      ix = 0;
    } else {
      ix = idx;
    }

    c_st.site = &k_emlrtRSI;
    overflow = !(ii->size[0] != 1);
    guard4 = false;
    if (overflow) {
      overflow = false;
      if (ix != 1) {
        overflow = true;
      }

      if (overflow) {
        overflow = true;
      } else {
        guard4 = true;
      }
    } else {
      guard4 = true;
    }

    if (guard4) {
      overflow = false;
    }

    d_st.site = &m_emlrtRSI;
    if (!overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&d_st, &t_emlrtRTEI,
        "Coder:FE:PotentialVectorVector", 0);
    }

    nx = ii->size[0];
    ii->size[0] = ix;
    emxEnsureCapacity(&b_st, (emxArray__common *)ii, nx, (int32_T)sizeof(int32_T),
                      &c_emlrtRTEI);
  }

  emxInit_real_T(&b_st, &newlyoccidx, 1, &f_emlrtRTEI, true);
  ix = newlyoccidx->size[0];
  newlyoccidx->size[0] = ii->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)newlyoccidx, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = ii->size[0];
  for (ix = 0; ix < idx; ix++) {
    newlyoccidx->data[ix] = ii->data[ix];
  }

  st.site = &b_emlrtRSI;
  ix = x->size[0];
  x->size[0] = cgridvec->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (cgridvec->data[ix] == occval);
  }

  b_st.site = &i_emlrtRSI;
  nx = x->size[0];
  idx = 0;
  ix = ii->size[0];
  ii->size[0] = x->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                    &emlrtRTEI);
  c_st.site = &j_emlrtRSI;
  if (1 > x->size[0]) {
    overflow = false;
  } else {
    overflow = (x->size[0] > 2147483646);
  }

  if (overflow) {
    d_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&d_st);
  }

  iy = 1;
  exitg5 = false;
  while ((!exitg5) && (iy <= nx)) {
    guard2 = false;
    if (x->data[iy - 1]) {
      idx++;
      ii->data[idx - 1] = iy;
      if (idx >= nx) {
        exitg5 = true;
      } else {
        guard2 = true;
      }
    } else {
      guard2 = true;
    }

    if (guard2) {
      iy++;
    }
  }

  if (idx <= x->size[0]) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &s_emlrtRTEI,
      "Coder:builtins:AssertionFailed", 0);
  }

  if (x->size[0] == 1) {
    if (idx == 0) {
      ix = ii->size[0];
      ii->size[0] = 0;
      emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof
                        (int32_T), &emlrtRTEI);
    }
  } else {
    if (1 > idx) {
      ix = 0;
    } else {
      ix = idx;
    }

    c_st.site = &k_emlrtRSI;
    overflow = !(ii->size[0] != 1);
    b_guard3 = false;
    if (overflow) {
      overflow = false;
      if (ix != 1) {
        overflow = true;
      }

      if (overflow) {
        overflow = true;
      } else {
        b_guard3 = true;
      }
    } else {
      b_guard3 = true;
    }

    if (b_guard3) {
      overflow = false;
    }

    d_st.site = &m_emlrtRSI;
    if (!overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&d_st, &t_emlrtRTEI,
        "Coder:FE:PotentialVectorVector", 0);
    }

    nx = ii->size[0];
    ii->size[0] = ix;
    emxEnsureCapacity(&b_st, (emxArray__common *)ii, nx, (int32_T)sizeof(int32_T),
                      &c_emlrtRTEI);
  }

  emxInit_real_T(&b_st, &occidx, 1, &g_emlrtRTEI, true);
  ix = occidx->size[0];
  occidx->size[0] = ii->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)occidx, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = ii->size[0];
  for (ix = 0; ix < idx; ix++) {
    occidx->data[ix] = ii->data[ix];
  }

  st.site = &c_emlrtRSI;
  ix = x->size[0];
  x->size[0] = cgridvec->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)x, ix, (int32_T)sizeof(boolean_T),
                    &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    x->data[ix] = (cgridvec->data[ix] != occval);
  }

  b_st.site = &i_emlrtRSI;
  nx = x->size[0];
  idx = 0;
  ix = ii->size[0];
  ii->size[0] = x->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                    &emlrtRTEI);
  c_st.site = &j_emlrtRSI;
  if (1 > x->size[0]) {
    overflow = false;
  } else {
    overflow = (x->size[0] > 2147483646);
  }

  if (overflow) {
    d_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&d_st);
  }

  iy = 1;
  exitg4 = false;
  while ((!exitg4) && (iy <= nx)) {
    guard1 = false;
    if (x->data[iy - 1]) {
      idx++;
      ii->data[idx - 1] = iy;
      if (idx >= nx) {
        exitg4 = true;
      } else {
        guard1 = true;
      }
    } else {
      guard1 = true;
    }

    if (guard1) {
      iy++;
    }
  }

  if (idx <= x->size[0]) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &s_emlrtRTEI,
      "Coder:builtins:AssertionFailed", 0);
  }

  if (x->size[0] == 1) {
    if (idx == 0) {
      ix = ii->size[0];
      ii->size[0] = 0;
      emxEnsureCapacity(&b_st, (emxArray__common *)ii, ix, (int32_T)sizeof
                        (int32_T), &emlrtRTEI);
    }
  } else {
    if (1 > idx) {
      ix = 0;
    } else {
      ix = idx;
    }

    c_st.site = &k_emlrtRSI;
    overflow = !(ii->size[0] != 1);
    b_guard2 = false;
    if (overflow) {
      overflow = false;
      if (ix != 1) {
        overflow = true;
      }

      if (overflow) {
        overflow = true;
      } else {
        b_guard2 = true;
      }
    } else {
      b_guard2 = true;
    }

    if (b_guard2) {
      overflow = false;
    }

    d_st.site = &m_emlrtRSI;
    if (!overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&d_st, &t_emlrtRTEI,
        "Coder:FE:PotentialVectorVector", 0);
    }

    nx = ii->size[0];
    ii->size[0] = ix;
    emxEnsureCapacity(&b_st, (emxArray__common *)ii, nx, (int32_T)sizeof(int32_T),
                      &c_emlrtRTEI);
  }

  emxFree_boolean_T(&x);
  emxInit_real_T(&b_st, &noccidx, 1, &h_emlrtRTEI, true);
  ix = noccidx->size[0];
  noccidx->size[0] = ii->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)noccidx, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = ii->size[0];
  for (ix = 0; ix < idx; ix++) {
    noccidx->data[ix] = ii->data[ix];
  }

  nrnocc = noccidx->size[0] - 1;

  /*  1 Intensify newly occupied cells  */
  j = 0;
  emxInit_real_T1(sp, &curr_col, 2, &i_emlrtRTEI, true);
  emxInit_real_T1(sp, &updt_col, 2, &j_emlrtRTEI, true);
  emxInit_real_T1(sp, &z, 2, &emlrtRTEI, true);
  while (j <= newlyoccidx->size[0] - 1) {
    /*  For newly occupied cells  */
    ix = newlyoccidx->size[0];
    if (!((j + 1 >= 1) && (j + 1 <= ix))) {
      emlrtDynamicBoundsCheckR2012b(j + 1, 1, ix, &eb_emlrtBCI, sp);
    }

    coccidx = (int32_T)newlyoccidx->data[j] - 1;
    ix = context->size[0];
    nx = (int32_T)newlyoccidx->data[j];
    if (!((nx >= 1) && (nx <= ix))) {
      emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &emlrtBCI, sp);
    }

    st.site = &d_emlrtRSI;
    b_st.site = &n_emlrtRSI;
    c_st.site = &o_emlrtRSI;
    ix = context->size[1];
    b_guard1 = false;
    if (ix == 1) {
      b_guard1 = true;
    } else {
      ix = context->size[1];
      if (ix != 1) {
        b_guard1 = true;
      } else {
        overflow = false;
      }
    }

    if (b_guard1) {
      overflow = true;
    }

    if (overflow) {
    } else {
      emlrtErrorWithMessageIdR2012b(&c_st, &u_emlrtRTEI,
        "Coder:toolbox:autoDimIncompatibility", 0);
    }

    ix = context->size[1];
    if (ix > 0) {
    } else {
      emlrtErrorWithMessageIdR2012b(&c_st, &v_emlrtRTEI,
        "Coder:toolbox:eml_min_or_max_varDimZero", 0);
    }

    d_st.site = &p_emlrtRSI;
    ixstart = 1;
    n = context->size[1];
    nx = (int32_T)newlyoccidx->data[j];
    mtmp = context->data[nx - 1];
    ix = context->size[1];
    if (ix > 1) {
      if (muDoubleScalarIsNaN(mtmp)) {
        e_st.site = &r_emlrtRSI;
        ix = context->size[1];
        if (2 > ix) {
          overflow = false;
        } else {
          ix = context->size[1];
          overflow = (ix > 2147483646);
        }

        if (overflow) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        ix = 2;
        exitg3 = false;
        while ((!exitg3) && (ix <= n)) {
          ixstart = ix;
          if (!muDoubleScalarIsNaN(context->data[coccidx + context->size[0] *
               (ix - 1)])) {
            mtmp = context->data[coccidx + context->size[0] * (ix - 1)];
            exitg3 = true;
          } else {
            ix++;
          }
        }
      }

      ix = context->size[1];
      if (ixstart < ix) {
        e_st.site = &q_emlrtRSI;
        ix = context->size[1];
        if (ixstart + 1 > ix) {
          overflow = false;
        } else {
          ix = context->size[1];
          overflow = (ix > 2147483646);
        }

        if (overflow) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        for (ix = ixstart + 1; ix <= n; ix++) {
          if (context->data[coccidx + context->size[0] * (ix - 1)] > mtmp) {
            mtmp = context->data[coccidx + context->size[0] * (ix - 1)];
          }
        }
      }
    }

    if (mtmp < minthreshold) {
      idx = context->size[1];
      iy = context->size[0];
      nx = (int32_T)newlyoccidx->data[j];
      if (!((nx >= 1) && (nx <= iy))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, iy, &b_emlrtBCI, sp);
      }

      for (ix = 0; ix < idx; ix++) {
        context->data[(nx + context->size[0] * ix) - 1] = reinitval;
      }

      /*  Reinitialize */
    } else {
      idx = context->size[1];
      nx = (int32_T)newlyoccidx->data[j];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        updt_col->data[updt_col->size[0] * ix] = intensifyrate * context->data
          [(nx + context->size[0] * ix) - 1];
      }

      /*  Intensify */
      st.site = &e_emlrtRSI;
      b_st.site = &s_emlrtRSI;
      c_st.site = &o_emlrtRSI;
      d_st.site = &t_emlrtRSI;
      ix = curr_col->size[0] * curr_col->size[1];
      curr_col->size[0] = 1;
      curr_col->size[1] = updt_col->size[1];
      emxEnsureCapacity(&d_st, (emxArray__common *)curr_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      idx = updt_col->size[0] * updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        curr_col->data[ix] = updt_col->data[ix];
      }

      e_st.site = &u_emlrtRSI;
      for (ix = 0; ix < 2; ix++) {
        varargin_1[ix] = updt_col->size[ix];
      }

      ix = z->size[0] * z->size[1];
      z->size[0] = 1;
      z->size[1] = updt_col->size[1];
      emxEnsureCapacity(&e_st, (emxArray__common *)z, ix, (int32_T)sizeof(real_T),
                        &d_emlrtRTEI);
      iy = updt_col->size[1];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = varargin_1[1];
      emxEnsureCapacity(&e_st, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &e_emlrtRTEI);
      if (dimagree(updt_col, curr_col)) {
      } else {
        emlrtErrorWithMessageIdR2012b(&e_st, &x_emlrtRTEI, "MATLAB:dimagree", 0);
      }

      e_st.site = &v_emlrtRSI;
      if (1 > z->size[1]) {
        overflow = false;
      } else {
        overflow = (z->size[1] > 2147483646);
      }

      if (overflow) {
        f_st.site = &l_emlrtRSI;
        check_forloop_overflow_error(&f_st);
      }

      for (k = 0; k + 1 <= iy; k++) {
        updt_col->data[k] = muDoubleScalarMin(curr_col->data[k], maxthreshold);
      }

      /*  Max-thesholding */
      ix = context->size[0];
      nx = (int32_T)newlyoccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &c_emlrtBCI, sp);
      }

      idx = context->size[1];
      ix = ii->size[0];
      ii->size[0] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                        &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        ii->data[ix] = ix;
      }

      iv3[0] = 1;
      iv3[1] = ii->size[0];
      emlrtSubAssignSizeCheckR2012b(iv3, 2, *(int32_T (*)[2])updt_col->size, 2,
        &b_emlrtECI, sp);
      nx = (int32_T)newlyoccidx->data[j];
      idx = updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        context->data[(nx + context->size[0] * ii->data[ix]) - 1] =
          updt_col->data[updt_col->size[0] * ix];
      }
    }

    j++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }

  emxFree_real_T(&z);

  /*  2 Attenuate unoccupied cells */
  if (do_attenuation_first == 1.0) {
    j = 0;
    while (j <= nrnocc) {
      /*  For unoccupied cells */
      ix = noccidx->size[0];
      nx = j + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &d_emlrtBCI, sp);
      }

      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &e_emlrtBCI, sp);
      }

      idx = context->size[1];
      iy = (int32_T)noccidx->data[j];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        updt_col->data[updt_col->size[0] * ix] = context->data[(iy +
          context->size[0] * ix) - 1] * nocc_attenuaterate;
      }

      /*  Attenuate */
      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &f_emlrtBCI, sp);
      }

      idx = context->size[1];
      ix = ii->size[0];
      ii->size[0] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                        &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        ii->data[ix] = ix;
      }

      iv4[0] = 1;
      iv4[1] = ii->size[0];
      emlrtSubAssignSizeCheckR2012b(iv4, 2, *(int32_T (*)[2])updt_col->size, 2,
        &c_emlrtECI, sp);
      iy = (int32_T)noccidx->data[j];
      idx = updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        context->data[(iy + context->size[0] * ii->data[ix]) - 1] =
          updt_col->data[updt_col->size[0] * ix];
      }

      j++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }
  }

  /*  4 Propagation  */
  j = 0;
  while (j <= occidx->size[0] - 1) {
    /*  For occupied cells  */
    ix = occidx->size[0];
    if (!((j + 1 >= 1) && (j + 1 <= ix))) {
      emlrtDynamicBoundsCheckR2012b(j + 1, 1, ix, &bb_emlrtBCI, sp);
    }

    idx = context->size[1];
    ix = context->size[0];
    iy = (int32_T)occidx->data[j];
    if (!((iy >= 1) && (iy <= ix))) {
      emlrtDynamicBoundsCheckR2012b(iy, 1, ix, &g_emlrtBCI, sp);
    }

    ix = curr_col->size[0] * curr_col->size[1];
    curr_col->size[0] = 1;
    curr_col->size[1] = idx;
    emxEnsureCapacity(sp, (emxArray__common *)curr_col, ix, (int32_T)sizeof
                      (real_T), &emlrtRTEI);
    for (ix = 0; ix < idx; ix++) {
      curr_col->data[curr_col->size[0] * ix] = context->data[(iy + context->
        size[0] * ix) - 1];
    }

    ix = nei_idx->size[0];
    nx = (int32_T)occidx->data[j];
    if (!((nx >= 1) && (nx <= ix))) {
      emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &h_emlrtBCI, sp);
    }

    ix = nei_weight->size[0];
    nx = (int32_T)occidx->data[j];
    if (!((nx >= 1) && (nx <= ix))) {
      emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &i_emlrtBCI, sp);
    }

    emlrtForLoopVectorCheckR2012b(1.0, 1.0, nei_filter_n, mxDOUBLE_CLASS,
      (int32_T)nei_filter_n, &p_emlrtRTEI, sp);
    k = 0;
    while (k <= (int32_T)nei_filter_n - 1) {
      /*  For all neighbor cells  */
      ix = curr_col->size[1];
      nx = k + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &j_emlrtBCI, sp);
      }

      ix = nei_idx->size[1];
      nx = k + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &k_emlrtBCI, sp);
      }

      ix = nei_weight->size[1];
      nx = k + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &l_emlrtBCI, sp);
      }

      iy = (int32_T)occidx->data[j];
      if (nei_idx->data[(iy + nei_idx->size[0] * k) - 1] != 0.0) {
        /*  If properly connected, propagate */
        iy = (int32_T)occidx->data[j];
        d0 = nei_idx->data[(iy + nei_idx->size[0] * k) - 1];
        if (d0 != (int32_T)muDoubleScalarFloor(d0)) {
          emlrtIntegerCheckR2012b(d0, &emlrtDCI, sp);
        }

        ix = context->size[0];
        nx = (int32_T)d0;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &m_emlrtBCI, sp);
        }

        ix = context->size[1];
        nx = k + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &n_emlrtBCI, sp);
        }

        /*  Maximum value thresholding  */
        iy = (int32_T)occidx->data[j];
        idx = (int32_T)occidx->data[j];
        nx = (int32_T)occidx->data[j];
        ix = context->size[0];
        nx = (int32_T)nei_idx->data[(nx + nei_idx->size[0] * k) - 1];
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &cb_emlrtBCI, sp);
        }

        ix = context->size[1];
        if (!((k + 1 >= 1) && (k + 1 <= ix))) {
          emlrtDynamicBoundsCheckR2012b(k + 1, 1, ix, &db_emlrtBCI, sp);
        }

        context->data[(nx + context->size[0] * k) - 1] = muDoubleScalarMax
          (context->data[((int32_T)nei_idx->data[(iy + nei_idx->size[0] * k) - 1]
                          + context->size[0] * k) - 1], muDoubleScalarMin
           (nei_weight->data[(idx + nei_weight->size[0] * k) - 1] *
            curr_col->data[k], maxthreshold));

        /*  Make sure current context propagation does not weaken the flow information */
      }

      k++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }

    j++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }

  emxFree_real_T(&occidx);
  emxInit_real_T1(sp, &tempcontext, 2, &k_emlrtRTEI, true);

  /*  5 Uncertainty in acceleration */
  ix = tempcontext->size[0] * tempcontext->size[1];
  tempcontext->size[0] = context->size[0];
  tempcontext->size[1] = context->size[1];
  emxEnsureCapacity(sp, (emxArray__common *)tempcontext, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = context->size[0] * context->size[1];
  for (ix = 0; ix < idx; ix++) {
    tempcontext->data[ix] = context->data[ix];
  }

  emlrtForLoopVectorCheckR2012b(1.0, 1.0, nei_filter_n, mxDOUBLE_CLASS, (int32_T)
    nei_filter_n, &q_emlrtRTEI, sp);
  j = 0;
  emxInit_real_T1(sp, &b_nei4u_weight, 2, &emlrtRTEI, true);
  while (j <= (int32_T)nei_filter_n - 1) {
    /*  For all context level */
    k = 0;
    while (k <= nei_idx->size[0] - 1) {
      /*  For all cells */
      sumval = 0.0;
      emlrtForLoopVectorCheckR2012b(1.0, 1.0, nei4u_filter_n, mxDOUBLE_CLASS,
        (int32_T)nei4u_filter_n, &r_emlrtRTEI, sp);
      m = 0;
      while (m <= (int32_T)nei4u_filter_n - 1) {
        ix = nei4u_idx->size[0];
        nx = k + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &o_emlrtBCI, sp);
        }

        ix = nei4u_idx->size[1];
        nx = m + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &p_emlrtBCI, sp);
        }

        ix = nei4u_weight->size[0];
        nx = k + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &q_emlrtBCI, sp);
        }

        ix = nei4u_weight->size[1];
        nx = m + 1;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &r_emlrtBCI, sp);
        }

        idx = nei4u_weight->size[1];
        ix = nei4u_weight->size[0];
        nx = 1 + k;
        if (!((nx >= 1) && (nx <= ix))) {
          emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &s_emlrtBCI, sp);
        }

        ix = b_nei4u_weight->size[0] * b_nei4u_weight->size[1];
        b_nei4u_weight->size[0] = 1;
        b_nei4u_weight->size[1] = idx;
        emxEnsureCapacity(sp, (emxArray__common *)b_nei4u_weight, ix, (int32_T)
                          sizeof(real_T), &emlrtRTEI);
        for (ix = 0; ix < idx; ix++) {
          b_nei4u_weight->data[b_nei4u_weight->size[0] * ix] =
            nei4u_weight->data[(nx + nei4u_weight->size[0] * ix) - 1];
        }

        st.site = &f_emlrtRSI;
        mtmp = sum(&st, b_nei4u_weight);
        if (nei4u_idx->data[k + nei4u_idx->size[0] * m] != 0.0) {
          d0 = nei4u_idx->data[k + nei4u_idx->size[0] * m];
          if (d0 != (int32_T)muDoubleScalarFloor(d0)) {
            emlrtIntegerCheckR2012b(d0, &b_emlrtDCI, sp);
          }

          ix = context->size[0];
          nx = (int32_T)d0;
          if (!((nx >= 1) && (nx <= ix))) {
            emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &t_emlrtBCI, sp);
          }

          ix = context->size[1];
          nx = j + 1;
          if (!((nx >= 1) && (nx <= ix))) {
            emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &u_emlrtBCI, sp);
          }

          sumval += nei4u_weight->data[k + nei4u_weight->size[0] * m] / mtmp *
            context->data[((int32_T)nei4u_idx->data[k + nei4u_idx->size[0] * m]
                           + context->size[0] * j) - 1];
        }

        m++;
        if (*emlrtBreakCheckR2012bFlagVar != 0) {
          emlrtBreakCheckR2012b(sp);
        }
      }

      ix = tempcontext->size[0];
      nx = 1 + k;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &y_emlrtBCI, sp);
      }

      ix = tempcontext->size[1];
      if (!((j + 1 >= 1) && (j + 1 <= ix))) {
        emlrtDynamicBoundsCheckR2012b(j + 1, 1, ix, &ab_emlrtBCI, sp);
      }

      tempcontext->data[(nx + tempcontext->size[0] * j) - 1] = sumval;
      k++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }

    j++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }

  emxFree_real_T(&b_nei4u_weight);
  ix = context->size[0] * context->size[1];
  context->size[0] = tempcontext->size[0];
  context->size[1] = tempcontext->size[1];
  emxEnsureCapacity(sp, (emxArray__common *)context, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = tempcontext->size[1];
  for (ix = 0; ix < idx; ix++) {
    iy = tempcontext->size[0];
    for (nx = 0; nx < iy; nx++) {
      context->data[nx + context->size[0] * ix] = tempcontext->data[nx +
        tempcontext->size[0] * ix];
    }
  }

  if (do_attenuation_first == 0.0) {
    /*  2 Attenuate unoccupied cells */
    j = 0;
    while (j <= nrnocc) {
      /*  For unoccupied cells, attenuate */
      ix = noccidx->size[0];
      nx = j + 1;
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &v_emlrtBCI, sp);
      }

      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &w_emlrtBCI, sp);
      }

      idx = context->size[1];
      iy = (int32_T)noccidx->data[j];
      ix = updt_col->size[0] * updt_col->size[1];
      updt_col->size[0] = 1;
      updt_col->size[1] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)updt_col, ix, (int32_T)sizeof
                        (real_T), &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        updt_col->data[updt_col->size[0] * ix] = context->data[(iy +
          context->size[0] * ix) - 1] * nocc_attenuaterate;
      }

      ix = context->size[0];
      nx = (int32_T)noccidx->data[j];
      if (!((nx >= 1) && (nx <= ix))) {
        emlrtDynamicBoundsCheckR2012b(nx, 1, ix, &x_emlrtBCI, sp);
      }

      idx = context->size[1];
      ix = ii->size[0];
      ii->size[0] = idx;
      emxEnsureCapacity(sp, (emxArray__common *)ii, ix, (int32_T)sizeof(int32_T),
                        &emlrtRTEI);
      for (ix = 0; ix < idx; ix++) {
        ii->data[ix] = ix;
      }

      iv5[0] = 1;
      iv5[1] = ii->size[0];
      emlrtSubAssignSizeCheckR2012b(iv5, 2, *(int32_T (*)[2])updt_col->size, 2,
        &d_emlrtECI, sp);
      iy = (int32_T)noccidx->data[j];
      idx = updt_col->size[1];
      for (ix = 0; ix < idx; ix++) {
        context->data[(iy + context->size[0] * ii->data[ix]) - 1] =
          updt_col->data[updt_col->size[0] * ix];
      }

      j++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }
  }

  emxFree_int32_T(&ii);
  emxFree_real_T(&updt_col);
  emxFree_real_T(&noccidx);

  /*  6 Prediction */
  st.site = &g_emlrtRSI;
  ix = tempcontext->size[0] * tempcontext->size[1];
  tempcontext->size[0] = context->size[1];
  tempcontext->size[1] = context->size[0];
  emxEnsureCapacity(&st, (emxArray__common *)tempcontext, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = context->size[0];
  for (ix = 0; ix < idx; ix++) {
    iy = context->size[1];
    for (nx = 0; nx < iy; nx++) {
      tempcontext->data[nx + tempcontext->size[0] * ix] = context->data[ix +
        context->size[0] * nx];
    }
  }

  b_st.site = &n_emlrtRSI;
  c_st.site = &o_emlrtRSI;
  if (((tempcontext->size[0] == 1) && (tempcontext->size[1] == 1)) ||
      (tempcontext->size[0] != 1)) {
    overflow = true;
  } else {
    overflow = false;
  }

  if (overflow) {
  } else {
    emlrtErrorWithMessageIdR2012b(&c_st, &u_emlrtRTEI,
      "Coder:toolbox:autoDimIncompatibility", 0);
  }

  if (tempcontext->size[0] > 0) {
  } else {
    emlrtErrorWithMessageIdR2012b(&c_st, &v_emlrtRTEI,
      "Coder:toolbox:eml_min_or_max_varDimZero", 0);
  }

  ix = curr_col->size[0] * curr_col->size[1];
  curr_col->size[0] = 1;
  curr_col->size[1] = tempcontext->size[1];
  emxEnsureCapacity(&c_st, (emxArray__common *)curr_col, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  n = tempcontext->size[0];
  ix = 0;
  iy = -1;
  d_st.site = &ab_emlrtRSI;
  if (1 > tempcontext->size[1]) {
    overflow = false;
  } else {
    overflow = (tempcontext->size[1] > 2147483646);
  }

  if (overflow) {
    e_st.site = &l_emlrtRSI;
    check_forloop_overflow_error(&e_st);
  }

  for (nx = 1; nx <= tempcontext->size[1]; nx++) {
    d_st.site = &bb_emlrtRSI;
    ixstart = ix;
    idx = ix + n;
    mtmp = tempcontext->data[ix];
    if (n > 1) {
      if (muDoubleScalarIsNaN(tempcontext->data[ix])) {
        e_st.site = &r_emlrtRSI;
        if (ix + 2 > idx) {
          b_ix = false;
        } else {
          b_ix = (idx > 2147483646);
        }

        if (b_ix) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        k = ix + 1;
        exitg2 = false;
        while ((!exitg2) && (k + 1 <= idx)) {
          ixstart = k;
          if (!muDoubleScalarIsNaN(tempcontext->data[k])) {
            mtmp = tempcontext->data[k];
            exitg2 = true;
          } else {
            k++;
          }
        }
      }

      if (ixstart + 1 < idx) {
        e_st.site = &q_emlrtRSI;
        if (ixstart + 2 > idx) {
          b_ixstart = false;
        } else {
          b_ixstart = (idx > 2147483646);
        }

        if (b_ixstart) {
          f_st.site = &l_emlrtRSI;
          check_forloop_overflow_error(&f_st);
        }

        for (k = ixstart + 1; k + 1 <= idx; k++) {
          if (tempcontext->data[k] > mtmp) {
            mtmp = tempcontext->data[k];
          }
        }
      }
    }

    iy++;
    curr_col->data[iy] = mtmp;
    ix += n;
  }

  emxFree_real_T(&tempcontext);
  ix = maxvec->size[0];
  maxvec->size[0] = curr_col->size[1];
  emxEnsureCapacity(sp, (emxArray__common *)maxvec, ix, (int32_T)sizeof(real_T),
                    &emlrtRTEI);
  idx = curr_col->size[1];
  for (ix = 0; ix < idx; ix++) {
    maxvec->data[ix] = curr_col->data[curr_col->size[0] * ix];
  }

  emxFree_real_T(&curr_col);
  st.site = &h_emlrtRSI;

  /*  sigm_a  <= if we increase the value, than the sigm function gets peaky! */
  b_st.site = &cb_emlrtRSI;
  ix = predvec->size[0];
  predvec->size[0] = maxvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)predvec, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = maxvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    predvec->data[ix] = -sigm_coef * maxvec->data[ix];
  }

  c_st.site = &cb_emlrtRSI;
  b_exp(&c_st, predvec);
  ix = predvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)predvec, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = predvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    predvec->data[ix] = 1.0 - predvec->data[ix];
  }

  ix = newlyoccidx->size[0];
  newlyoccidx->size[0] = maxvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)newlyoccidx, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = maxvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    newlyoccidx->data[ix] = -sigm_coef * maxvec->data[ix];
  }

  c_st.site = &cb_emlrtRSI;
  b_exp(&c_st, newlyoccidx);
  ix = newlyoccidx->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)newlyoccidx, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = newlyoccidx->size[0];
  for (ix = 0; ix < idx; ix++) {
    newlyoccidx->data[ix]++;
  }

  varargin_1[0] = predvec->size[0];
  varargin_1[1] = 1;
  varargin_2[0] = newlyoccidx->size[0];
  varargin_2[1] = 1;
  overflow = false;
  p = true;
  k = 0;
  exitg1 = false;
  while ((!exitg1) && (k < 2)) {
    if (!(varargin_1[k] == varargin_2[k])) {
      p = false;
      exitg1 = true;
    } else {
      k++;
    }
  }

  if (!p) {
  } else {
    overflow = true;
  }

  if (overflow) {
  } else {
    emlrtErrorWithMessageIdR2012b(&b_st, &w_emlrtRTEI, "MATLAB:dimagree", 0);
  }

  ix = predvec->size[0];
  emxEnsureCapacity(&b_st, (emxArray__common *)predvec, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = predvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    predvec->data[ix] /= newlyoccidx->data[ix];
  }

  emxFree_real_T(&newlyoccidx);

  /*  7 Save previous grid */
  ix = cgridvecprev->size[0];
  cgridvecprev->size[0] = cgridvec->size[0];
  emxEnsureCapacity(sp, (emxArray__common *)cgridvecprev, ix, (int32_T)sizeof
                    (real_T), &emlrtRTEI);
  idx = cgridvec->size[0];
  for (ix = 0; ix < idx; ix++) {
    cgridvecprev->data[ix] = cgridvec->data[ix];
  }

  emlrtHeapReferenceStackLeaveFcnR2012b(sp);
}