使用MATLAB Coder转换MATLAB unique函数至C++时的代码实现疑问
std::unique for my MATLAB unique function? My Scenario
I tried converting this simple MATLAB function to C++ using MATLAB Coder:
function uniqueC_conversion() a= [1 0 1 2 4 5 1] b=unique(a) end
But instead of getting code that uses std::unique (like the C++ standard library function), I got this C-style implementation:
// // 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 was expecting it to leverage std::unique from the C++ standard library, so I'm confused why it produced this C-style code instead.
Answers
Great question! There are a few key reasons MATLAB Coder avoids std::unique here, all tied to matching MATLAB's behavior exactly and maximizing code portability:
MATLAB's
uniquedoes more thanstd::unique
The C++std::uniqueonly removes consecutive duplicate elements and leaves the array unsorted. MATLAB'suniqueby default returns a sorted array of unique values—that's what theiv1index array is doing: reordering your originalainto sorted order[0,1,1,1,2,4,5]. Sincestd::uniquecan't handle the sorting step, MATLAB Coder has to implement the full sort+dedup logic itself.Numerical precision matching
Look closely at the generated code: it includes logic usingfrexp,ldexp, and floating-point comparisons. MATLAB'suniqueuses a tolerance-based equality check (even for integers, since MATLAB represents all numeric values as floats by default) instead of strict integer equality.std::uniqueuses strict==comparisons, which wouldn't replicate MATLAB's behavior for edge cases like nearly-equal floating-point numbers. The custom code ensures the equality check matches MATLAB's definition exactly.Portability and minimal dependencies
MATLAB Coder is designed to generate code that works in all sorts of environments, including embedded systems where the full C++ standard library might not be available. By avoidingstd::unique(and other STL functions), the generated code is self-contained and doesn't rely on external library implementations that might vary between compilers or platforms. This is a deliberate choice to make the code as robust as possible across deployment targets.Code generation philosophy
MathWorks' code generators prioritize control over implementation details to ensure consistency with MATLAB runtime behavior. Using hand-rolled logic instead of STL functions lets them fine-tune every aspect of the code to match MATLAB's exact semantics, rather than relying on potentially varying STL implementations.
If you want to use STL functions anyway...
If you're targeting an environment with a full C++ standard library and want to force more STL usage, you can check MATLAB Coder's configuration settings:
- Enable the "Use C++ Standard Library" option in the code generation settings
- Use
coder.cevalto explicitly callstd::uniquein your MATLAB code (though you'll have to handle sorting and equality checks yourself to match MATLAB's behavior)
Just keep in mind that doing this might break behavior parity with MATLAB, so test carefully!
内容的提问来源于stack exchange,提问作者mir

