C ++生成的代码(来自MATLAB)

时间:2011-06-23 20:39:16

标签: c++ matlab type-declaration

所以我从MATLAB生成了一些代码,但我不知道如何使用它。我的意思是如果我想要生成15(num2Gen)随机数并执行sum / avg计算,我应该如何声明* a和* avg然后调用fxn?

 void randAvg(uint16_T num2Gen, emxArray_uint16_T *a, uint16_T *avg)
    {
      int32_T i0;
      int32_T loop_ub;
      uint16_T sum;
      uint16_T i;
      real_T varargin_1;
      uint16_T u0;
      uint32_T u1;

      /* RANDAVG Summary of this function goes here */
      /*    Detailed explanation goes here */
      /* 'randAvg:4' a = fi(zeros(num2Gen,1),0,16,2); */
      i0 = a->size[0];
      a->size[0] = num2Gen;
      emxEnsureCapacity((emxArray__common *)a, i0, (int32_T)sizeof(uint16_T));
      loop_ub = num2Gen - 1;
      for (i0 = 0; i0 <= loop_ub; i0++) {
        a->data[i0] = 0;
      }

      /* 'randAvg:5' sum = fi(0,0,16,2); */
      sum = 0;

      /* 'randAvg:6' for i=1:num2Gen */
      for (i = 1; i <= num2Gen; i = (uint16_T)((uint32_T)i + 1U)) {
        /* 'randAvg:7' a(i) = fi((50 + 100.*rand(1)),0,16,2); */
        varargin_1 = 50.0 + 100.0 * b_rand();
        varargin_1 = ldexp(varargin_1, 2);
        varargin_1 = floor(varargin_1 + 0.5);
        if (varargin_1 < 65536.0) {
          if (varargin_1 >= 0.0) {
            u0 = (uint16_T)varargin_1;
          } else {
            u0 = 0;
          }
        } else {
          u0 = MAX_uint16_T;
        }

        a->data[i - 1] = u0;

        /* 'randAvg:8' sum = fi((sum + fi(a(i),0,16,2)),0,16,2); */
        u1 = (uint32_T)sum + (uint32_T)a->data[i - 1];
        if (u1 > 65535U) {
          u1 = 65535U;
        }

        sum = (uint16_T)u1;

        /* don't use the array look up twice... */
      }

      /* 'randAvg:10' avg = fi(sum/num2Gen,0,16,2); */
      *avg = (uint16_T)div_u32_near((uint32_T)sum, (uint32_T)num2Gen);
    }

谢谢!

1 个答案:

答案 0 :(得分:2)

我不了解MATLAB,但回答关于如何传递对函数的引用的问题:

emxArray_uint16_T a = <put something here>
uint16_T avg = <put something here>

...
randAvg(num2Gen, &a, &avg);