3-5 ??二維?陣??MEX 檔?範?

`NШӽdҡAHKGx}iBzA

Ĥ@ӽdҬO arrayx2.cAiNJGx}]i]t곡M곡^H 2A䤺epUG

lɡ]03-ε{/arrayx2.c^G]ǦϰUYi^
/* 禡 MATLAB  MEX ɮסAJ@ӦVqAXh2HVqC */

#include "mex.h"	/* mex.h ]t mxArray cwqAHΨLT */

/* prhs = pointer to the right-hand-side argumentsAYVJܼƦC */
/* prls = pointer to the  left-hand-side argumentsAYVXܼƦC */
#define IN  prhs[0]	/* wq IN  禡Ĥ@ӿJܼơAHK */
#define OUT plhs[0]	/* wq OUT 禡Ĥ@ӿXܼơAHK */

/* 禡\ରNVq x C@ӤH2AANGe y VqC */
/* 禡N|Q mexFunction ҩIsC */
void timestwo(double y[], double x[], int length) {
	int i;
	for (i=0; i<length; i++)
		y[i] = 2.0*x[i];
}

/* 禡M MATLAB qDn禡 */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] ) {
	double *xr, *xi, *yr, *yi;
	int length = mxGetM(IN)*mxGetN(IN);
  
	/* ˬdXMJܼƭӼƬO_O1A䤤		  */
	/* nrhs = no. of right-hand-side arguments]JܼƭӼơ^*/
	/* nlhs = no. of  left-hand-side arguments]XܼƭӼơ^*/
	if(nrhs!=1)	/* ˬdJܼƭӼƬO_O1 */
		mexErrMsgTxt("One input required.");
	if(nlhs>1)	/* ˬdXܼƭӼƬO_O1 */
		mexErrMsgTxt("Too many output arguments");
  
	/* ˬdJܼƬO_X */
	if(!mxIsDouble(IN))		/* ˬdJܼƬO_ double */
		mexErrMsgTxt("Input must be a double.");
  
	/* tmO鵹Xܼ */
	OUT = mxCreateDoubleMatrix(mxGetM(IN), mxGetN(IN), mxCOMPLEX);
  
	/* oJMXܼƪ곡 */
	xr = mxGetPr(IN);
	yr = mxGetPr(OUT);
	/* oJMXܼƪ곡 */
	if (mxIsComplex(IN)) {
		xi = mxGetPi(IN);
		yi = mxGetPi(OUT);
	}
  
	/* ڪBGNJVq곡H2 */
	timestwo(yr, xr, length);
	/* ڪBGNJVq곡H2 */
	if (mxIsComplex(IN))
		timestwo(yi, xi, length);
}

Hint
ѩҦ}Cb MATLAB ܦ@ӪVq]ҦpAGx}ӻANO@ӥѦUӦVqҦ걵ӦVq^A]bWzdҤAڭ̥i length = mxGetM(IN)*mxGetN(IN) ӨoVqסC

ۡAڭ̥iHsĶHW{G

>> mex arrayx2.c

T{iɪsbAiJpUG

>> which arrayx2 D:\matlabBook\MATLAB{]pGig\03-ε{\arrayx2.mexw64

iաAiJpUG

>> arrayx2([1 2 3]) ans = 2 4 6 >> arrayx2([1+i 2+2i 3+3i; 4 5 6]) ans = 2.0000 + 2.0000i 4.0000 + 4.0000i 6.0000 + 6.0000i 8.0000 10.0000 12.0000 >> arrayx2('Test string') ??? Input must be a double. `ĤG MEX dҬO pairdist.c]ɦWupairwise distancev²١^AiӿJx} A M BAx}jpOO pm pnAӯx}ON m ӪVq n ӪVqҧΦXAӨC@ӦVq׳O pC MEX ɮ׶Ǧ^@ӶZx} CA䤤 C(i, j)Vq A(:, i) MVq B(:, j) xC

Hint
jqƪBzA@몺DҳONUӦVqHVq覡令@ӯx}AAiUسBzC

pairdist.c {XpUG

lɡ]03-ε{/pairdist.c^G]ǦϰUYi^
#include <math.h>
#include "mex.h"	/* mex.h ]t mxArray cwqAHΨLT */

/* prhs = pointer to the right-hand-side argumentsAYVJܼƦC */
/* prls = pointer to the  left-hand-side argumentsAYVXܼƦC */
#define	MAT1 prhs[0]	/* wq MAT1 ƪĤ@ӿJܼơAHK */
#define	MAT2 prhs[1]	/* wq MAT1 ƪĤGӿJܼơAHK */
#define	OUT  plhs[0]	/* wq OUT  ƪĤ@ӿXܼơAHK */

/* ƬM MATLAB qDn */
void mexFunction(
	int nlhs,	mxArray *plhs[],
	int nrhs, const mxArray *prhs[]) {
	double	*out, *mat1, *mat2, squareSum;
	int m, p, n, p2, i, j, k;

	/* ˬdXܼƭӼƬO_2BJܼƭӼƬO_1A䤤	  */
	/* nrhs = no. of right-hand-side arguments]JܼƭӼơ^*/
	/* nlhs = no. of  left-hand-side arguments]XܼƭӼơ^*/
	if (nrhs!=2)
		mexErrMsgTxt("PAIRDIST requires two input arguments.");

	/* ˬd׬O_ŦXnD */
	p  = mxGetM(MAT1);
	m  = mxGetN(MAT1);
	p2  = mxGetM(MAT2);
	n = mxGetN(MAT2);
	if (p != p2)
		mexErrMsgTxt("Matrix sizes mismatch!");

	/* ˬdJܼƸƫAO_ŦXnD */
	if (!mxIsNumeric(MAT1) || mxIsSparse(MAT1)  || !mxIsDouble(MAT1))
		mexErrMsgTxt("Input 1 is not a full numerical array!");
	if (!mxIsNumeric(MAT2) || mxIsSparse(MAT2)  || !mxIsDouble(MAT2))
		mexErrMsgTxt("Input 2 is not a full numerical array!");

	/* tmO鵹Xܼ */
	OUT = mxCreateDoubleMatrix(m, n, mxREAL);

	/* oJMXܼƪ곡 */
	out = mxGetPr(OUT);
	mat1 = mxGetPr(MAT1);
	mat2 = mxGetPr(MAT2);

	/* ڭpⳡ */
	/* MAT1(i+1, j+1) ȬO mat1[j*m+i] */
	/* MAT2(i+1, j+1) ȬO mat2[j*n+i] */
	/*  OUT(i+1, j+1) ȬO  out[j*m+i] */

	for (i=0; i<m; i++)
		for (j=0; j<n; j++) {
			squareSum = 0;
			for (k=0; k<p; k++)
				squareSum += pow(mat1[i*p+k]-mat2[j*p+k], 2.0);
			out[j*m+i] = sqrt(squareSum);
		}
}

Hint
  • bWzdҤAЯSO`Nx}ޡAYϥ 0-based indexingAh MATLAB x} OUT(i+1, j+1) ҹȬO out[j*m+i]C
  • ]Zx}O٪A]YҼ{pįA﨤uHUiH﨤uHWAӤ@MC

ۡAڭ̥iHHW{isĶG

>> mex pairdist.c sĶ{AiզpUG >> which pairdist D:\matlabBook\MATLAB{]pGig\03-ε{\pairdist.mexw64 >> pairdist([2 3], [2 3 4]) ans = 0 1 2 1 0 1 >> A = rand(2, 3); >> B = rand(2, 4); >> C = pairdist(A, B) C = 0.5104 0.9806 0.4544 0.5649 0.3562 0.2347 0.5911 0.2628 0.2101 0.5891 0.2675 0.1639 >> pairdist('string1', 'string2') ??? Input 1 is not a full numerical array! `ҤЪdҡAOGx}iBzA䥦dҡAi MATLAB API UC
MATLAB{]pGig