mxMakeArrayReal(C)
실수 데이터를 유지하면서 복소수 mxArray
를 실수로 변환
C 구문
#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.
버전 내역
R2018a에 개발됨