Преобразовать комплекс mxArray к реальным, с сохранением реальных данных
#include "matrix.h" int mxMakeArrayReal(mxArray *pa);
Использовать mxMakeArrayReal преобразование комплекса mxArray к действительному mxArray. Массив содержит данные из действительной части исходного массива. Если оригинал mxArray является реальным, то функция ничего не делает.
Предположим, что ваше приложение определяет, что реальные числа являются единственным значимым результатом. Если сложные результаты возникают из-за шума в данных, то программа сбрасывает небольшие мнимые части. Однако если мнимая часть превышает порог, то программа выдает ошибку.
В следующем примере dropComplexIfUnderThreshold.c, пороговое значение устанавливается равным .2.
#include "mex.h"
/* dropComplexIfUnderThreshold converts input to a real double scalar
* with eihter no imaginary data or imaginary data less than
* the value of LIMIT.
*
* Use this function for data with imaginary values less than some LIMIT
* that can be dropped, and then revert the results to a real array.
*
* Usage: B = dropComplexIfUnderThreshold(A);
* Where:
* A is a mxDOUBLE_CLASS scalar complex or real.
* B is a real scalar which is a copy of the real value of A.
*
* Errors if:
* nlhs != 1
* nrhs != 1
* prhs[0] is not a mxDOUBLE_CLASS scalar
* imaginary data value is equal or greater than LIMIT
*
* Build:
* mex -R2018a dropComplexIfUnderThreshold.c - interleaved complex API
* mex [-R2017b] dropComplexIfUnderThreshold.c - separate complex API
*
* Run:
* >> dropComplexIfUnderThreshold(3)
* ans = 3
*
* >> dropComplexIfUnderThreshold(complex(3,.1))
* ans = 3
*
* >> dropComplexIfUnderThreshold(complex(1,.2))
* Error using dropComplexIfUnderThreshold
* Data error.
* >>
*/
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
#define LIMIT .2
/* check for the proper number of arguments */
if(nrhs != 1) {
mexErrMsgIdAndTxt("MATLAB:dropComplexIfUnderThreshold:checkrhs","1 input required.");
}
if(nlhs > 1) {
mexErrMsgIdAndTxt("MATLAB:dropComplexIfUnderThreshold:checklhs","Too many output arguments.");
}
if( !(mxIsDouble(prhs[0]) && mxIsScalar(prhs[0])) ) {
mexErrMsgIdAndTxt("MATLAB:dropComplexIfUnderThreshold:checkdouble","rhs[0] must be double scalar.");
}
plhs[0] = mxDuplicateArray(prhs[0]);
if(mxIsComplex(prhs[0])) {
#if MX_HAS_INTERLEAVED_COMPLEX
mxComplexDouble *dt = mxGetComplexDoubles(prhs[0]);
/* test imaginary data for significance */
if( dt[0].imag < LIMIT) {
mxMakeArrayReal(plhs[0]);
}
else {
mexErrMsgIdAndTxt("MATLAB:dropComplexIfUnderThreshold:outOfBounds","Data error.");
}
#else
mxDouble *dt = mxGetPi(plhs[0]);
/* test imaginary data for significance */
if (dt[0] < LIMIT) {
mxFree(mxGetPi(plhs[0]));
mxSetPi(plhs[0], 0);
} else {
mexErrMsgIdAndTxt("MATLAB:dropComplexIfUnderThreshold:outOfBounds","Data error.");
}
#endif
}
}Чтобы построить файл MEX, введите:
mex -R2018a dropComplexIfUnderThreshold.c
Для тестирования функции введите:
dropComplexIfUnderThreshold(3)
ans = 3
dropComplexIfUnderThreshold(complex(3,.1))
ans = 3
dropComplexIfUnderThreshold(complex(1,.2))
Error using dropComplexIfUnderThreshold Data error.