mycalc.c

来自「matlab7.0这本书的源代码」· C语言 代码 · 共 55 行

C
55
字号
/* * mycalc.c - calculates x^2 - x + 1/x for each element of an array. * *   MATLAB usage:   p = mycalc(n) * *   Mastering MATLAB 7 C MEX Example 2:  *       single 2-D real numeric array input,  *       single array output. */#include "mex.h"/* This is the original subroutine that performs the calculation. */static void mycalc( double p[], double n[], int r, int c){    int i;    for (i = 0; i < r*c; i++)        p[i] = n[i]*n[i]-n[i]+1.0/n[i];}/* This is the interface to MATLAB data types and arguments. */void mexFunction( int nlhs,       mxArray *plhs[], 		          int nrhs, const mxArray *prhs[] ){     double *p, *n;     int  r, c;         /* Do some error checking. */    if (nrhs != 1)          mexErrMsgTxt("One input argument required.");     else if (nlhs > 1)       mexErrMsgTxt("Too many output arguments.");     else if (!mxIsNumeric(prhs[0]))      mexErrMsgTxt("Input must be numeric.");     else if (mxIsComplex(prhs[0]))      mexErrMsgTxt("Input must be real.");     else if (mxGetNumberOfDimensions(prhs[0]) > 2)      mexErrMsgTxt("N-Dimensional arrays are not supported.");         /* Get the input array dimensions. */    r = mxGetM(prhs[0]);     c = mxGetN(prhs[0]);    /* Create a matrix for the return argument */     plhs[0] = mxCreateDoubleMatrix(r, c, mxREAL);         /* Assign pointers to the parameters */     p = mxGetPr(plhs[0]);    n = mxGetPr(prhs[0]);    /* Do the actual calculation in a subroutine. */    mycalc(p, n, r, c); }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?