¥»¸±N¤¶²Ð¨â­Ó½d¨Ò¡A¥H«K¹ï¤Gºû¯x°}¶i¦æ³B²z¡A

/* ¦¹¨ç¦¡¬° MATLAB ªº MEX ÀÉ®×¡A¨ä¿é¤J¬°¤@­Ó¦V¶q¡A¿é¥X«h¬°2­¼¥H¦¹¦V¶q¡C */

#include "mex.h"	/* mex.h ¥]§t mxArray µ²ºcªº©w¸q¡A¥H¤Î¨ä¥L¬ÛÃö¸ê°T */

/* prhs = pointer to the right-hand-side arguments¡A§Y«ü¦V¿é¤JÅÜ¼Æ¦Cªº«ü¼Ð */
/* prls = pointer to the  left-hand-side arguments¡A§Y«ü¦V¿é¥XÅÜ¼Æ¦Cªº«ü¼Ð */
#define IN  prhs[0]	/* ©w¸q IN  ¬°¦¹¨ç¦¡ªº²Ä¤@­Ó¿é¤JÅÜ¼Æ¡A¥H«K«áÄò¨ú¥Î */
#define OUT plhs[0]	/* ©w¸q OUT ¬°¦¹¨ç¦¡ªº²Ä¤@­Ó¿é¥XÅÜ¼Æ¡A¥H«K«áÄò¨ú¥Î */

/* ¦¹¨ç¦¡ªº¥\¯à¬°±N¦V¶q x ªº¨C¤@­Ó¤¸¯À­¼¥H2¡A¦A±Nµ²ªG°e¨ì y ¦V¶q¡C */
void timestwo(double y[], double x[], int length) {
int i;
for (i=0; i<length; i++)
y[i] = 2.0*x[i];
}

void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ) {
double *xr, *xi, *yr, *yi;
int length = mxGetM(IN)*mxGetN(IN);

/* nrhs = no. of right-hand-side arguments¡]¿é¤JÅÜ¼Æ­Ó¼Æ¡^*/
/* nlhs = no. of  left-hand-side arguments¡]¿é¥XÅÜ¼Æ­Ó¼Æ¡^*/
if(nrhs!=1)	/* ÀË¬d¿é¤JÅÜ¼Æ­Ó¼Æ¬O§_¬O1 */
mexErrMsgTxt("One input required.");
if(nlhs>1)	/* ÀË¬d¿é¥XÅÜ¼Æ­Ó¼Æ¬O§_¬O1 */
mexErrMsgTxt("Too many output arguments");

/* ÀË¬d¿é¤JÅÜ¼Æ¬O§_¦X®æ */
if(!mxIsDouble(IN))		/* ÀË¬d¿é¤JÅÜ¼Æ¬O§_¬° double */
mexErrMsgTxt("Input must be a double.");

/* °t¸m°O¾ÐÅéµ¹¿é¥XÅÜ¼Æ */
OUT = mxCreateDoubleMatrix(mxGetM(IN), mxGetN(IN), mxCOMPLEX);

xr = mxGetPr(IN);
yr = mxGetPr(OUT);
if (mxIsComplex(IN)) {
xi = mxGetPi(IN);
yi = mxGetPi(OUT);
}

/* °õ¦æ¹ê»Úªº¹Bºâ¡G±N¿é¤J¦V¶qªº¹ê³¡­¼¥H2 */
timestwo(yr, xr, length);
/* °õ¦æ¹ê»Úªº¹Bºâ¡G±N¿é¤J¦V¶qªºµê³¡­¼¥H2 */
if (mxIsComplex(IN))
timestwo(yi, xi, length);
}


Hint

±µµÛ¡A§Ú­Ì¥i¥H½sÄ¶¥H¤Wµ{¦¡¡G

>> mex arrayx2.c

±ý½T»{¥i°õ¦æÀÉªº¦s¦b¡A¥i¿é¤J¦p¤U¡G

>> which arrayx2 D:\matlabBook\MATLABµ{¦¡³]­p¡G¶i¶¥½g\03-À³¥Îµ{¦¡¤¶­±\arrayx2.mexw64

±ý¶i¦æ´ú¸Õ¡A¥i¿é¤J¦p¤U¡G

>> 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¡]ÀÉ¦W¬°¡upairwise distance¡vªºÂ²ºÙ¡^¡A¥i±µ¨ü¨â­Ó¿é¤J¯x°} A ©M B¡A¯x°}ªº¤j¤p¤À§O¬O p¡Ñm ¤Î p¡Ñn¡A¦¹¨â­Ó¯x°}¤À§O¥Nªí¥Ñ m ­Óª½¦æ¦V¶q¤Î n ­Óª½¦æ¦V¶q©Ò§Î¦¨ªº¶°¦X¡A¦Ó¨C¤@­Ó¦V¶qªºªø«×³£¬O p¡C¦¹ MEX ÀÉ®×¶Ç¦^¤@­Ó¶ZÂ÷¯x°} C¡A¨ä¤¤ C(i, j)¬°¦V¶q A(:, i) ©M¦V¶q B(:, j) ªº¯xÂ÷¡C

Hint

pairdist.c ªºµ{¦¡½X¦p¤U¡G

#include <math.h>
#include "mex.h"	/* mex.h ¥]§t mxArray µ²ºcªº©w¸q¡A¥H¤Î¨ä¥L¬ÛÃö¸ê°T */

/* prhs = pointer to the right-hand-side arguments¡A§Y«ü¦V¿é¤JÅÜ¼Æ¦Cªº«ü¼Ð */
/* prls = pointer to the  left-hand-side arguments¡A§Y«ü¦V¿é¥XÅÜ¼Æ¦Cªº«ü¼Ð */
#define	MAT1 prhs[0]	/* ©w¸q MAT1 ¬°¦¹¨ç¼Æªº²Ä¤@­Ó¿é¤JÅÜ¼Æ¡A¥H«K«áÄò¨ú¥Î */
#define	MAT2 prhs[1]	/* ©w¸q MAT1 ¬°¦¹¨ç¼Æªº²Ä¤G­Ó¿é¤JÅÜ¼Æ¡A¥H«K«áÄò¨ú¥Î */
#define	OUT  plhs[0]	/* ©w¸q OUT  ¬°¦¹¨ç¼Æªº²Ä¤@­Ó¿é¥XÅÜ¼Æ¡A¥H«K«áÄò¨ú¥Î */

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

/* ÀË¬d¿é¥XÅÜ¼Æ­Ó¼Æ¬O§_¬°2¡B¿é¤JÅÜ¼Æ­Ó¼Æ¬O§_¬°1¡A¨ä¤¤	  */
/* 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§_²Å¦X­n¨D */
p  = mxGetM(MAT1);
m  = mxGetN(MAT1);
p2  = mxGetM(MAT2);
n = mxGetN(MAT2);
if (p != p2)
mexErrMsgTxt("Matrix sizes mismatch!");

/* ÀË¬d¿é¤JÅÜ¼Æ¸ê®Æ«¬ºA¬O§_²Å¦X­n¨D */
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!");

/* °t¸m°O¾ÐÅéµ¹¿é¥XÅÜ¼Æ */
OUT = mxCreateDoubleMatrix(m, n, mxREAL);

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
• ¦b¤W­z½d¨Ò¤¤¡A½Ð¯S§Oª·N¯x°}ªº¯Á¤Þ¡A­Y¨Ï¥Î 0-based indexing¡A«h MATLAB ¯x°} OUT(i+1, j+1) ©Ò¹ïÀ³ªº¤¸¯À­È¬O out[j*m+i]¡C