0

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.

4
  • 2
    What is your question? Commented Apr 9, 2021 at 15:14
  • 1
    If the converter generated code that can be compiled by a C++ compiler, when you asked it to, that's about where its job ends. If you want it to do things in a more <algorithm>y way, try opening a feature request, if there's any way to do that. I don't see what SO can answer. Commented Apr 9, 2021 at 15:15
  • 2
    std::unique is not equivalent to MATLAB's unique anyway. Commented Apr 9, 2021 at 15:19
  • Thanks @beaker for clarification. The two unique functions are not the same. I guess this can explain the behavior. Commented Apr 9, 2021 at 16:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.