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 で導入