C++ conversion for unique function using MATLAB coder

150 Views Asked by At

I am trying to convert this simple MATLAB function to C++ using MATLAB coder:

function uniqueC_conversion()
a= [1 0 1 2 4 5 1]
b=unique(a)
end

The output is like this:

//
// uniqueCPP.cpp
//
// Code generation for function 'uniqueCPP'
//

// Include files
#include "uniqueCPP.h"
#include <cmath>
#include <math.h>

// Function Definitions
void uniqueCPP()
{
  static const signed char iv[7]{1, 0, 1, 2, 4, 5, 1};
  static const signed char iv1[7]{2, 1, 3, 7, 4, 5, 6};
  int exponent;
  int k;
  int nb;
  signed char b_data[7];
  for (k = 0; k < 7; k++) {
    b_data[k] = iv[iv1[k] - 1];
  }
  nb = -1;
  k = 1;
  while (k <= 7) {
    int x;
    x = b_data[k - 1];
    int exitg1;
    do {
      exitg1 = 0;
      k++;
      if (k > 7) {
        exitg1 = 1;
      } else {
        double absx;
        absx = static_cast<double>(x) / 2.0;
        if (absx <= 2.2250738585072014E-308) {
          absx = 4.94065645841247E-324;
        } else {
          frexp(absx, &exponent);
          absx = std::ldexp(1.0, exponent - 53);
        }
        if (!(std::abs(static_cast<double>(x - b_data[k - 1])) < absx)) {
          exitg1 = 1;
        }
      }
    } while (exitg1 == 0);
    nb++;
    b_data[nb] = static_cast<signed char>(x);
  }
}

// End of code generation (uniqueCPP.cpp)

I am puzzled since I expected the converted code uses the unique method as explained in this page: https://www.geeksforgeeks.org/stdunique-in-cpp/

The code I got is more a c conversion rather than a C++ conversion.

0

There are 0 best solutions below