Main Content

mxMakeArrayReal(C)

실수 데이터를 유지하면서 복소수 mxArray를 실수로 변환

C 구문

#include "matrix.h"
int mxMakeArrayReal(mxArray *pa);

설명

mxMakeArrayReal을 사용하여 복소수 mxArray를 실수 mxArray로 변환합니다. 배열에는 원래 배열의 실수부의 데이터가 포함됩니다. 원래 mxArray가 실수이면 함수는 아무런 작업도 수행하지 않습니다.

입력 인수

모두 확장

숫자형 mxArray 배열에 대한 포인터.

출력 인수

모두 확장

함수 상태로, int로 반환됩니다. 성공하는 경우, 함수는 1을 반환합니다.

실패할 경우 0을 반환합니다. paNULL이거나 숫자형이 아니거나 읽기 전용이면 함수는 실패합니다.

예제

애플리케이션이 실수만 의미 있는 결과로 판단한다고 가정하겠습니다. 데이터의 잡음으로 인해 복소수 결과가 발생하면 프로그램은 작은 허수부를 버립니다. 그러나 허수부가 임계값을 초과하면 프로그램은 오류를 발생시킵니다.

다음 예제 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에 개발됨

참고 항목