Ph.D. in Statistics. My researches are mainly devoted to parameter estimation techniques for nonlinear dynamical models with emphasis on stochastic differential equations.
trapzf does not allow for unevenly sampled data, even though the comment claims it does. Matlab's trapz does allow for uneven data. To correct this, the following change must be made to the C code of trapzf, here called trapzfu. Note that the speedup on a 64-bit WinXP computer of trapfzu over trapz was about 10-fold. Unrolling the loop in trapzfu2.c provides another 20% improvement in speed.
--- trapzfu.c ------
/* F=trapzfu(X,Y) computes the integral of Y with respect to X using
the trapezoidal method. X and Y must be vectors of the same length.
For trapzf to work properly:
X must be a STRICTLY monotonically increasing vector.
X does not need to be evenly spaced.
C. Quaia, National Eye Institute, NIH, DHHS
*/
#include "mex.h"
void trapzfu( double *area, double *xv, double *yv, int N) {
register int i;
*area = 0;
for (i = 0; i < (N-1); i++) {
*area = *area + (xv[i+1] - xv[i]) * (yv[i] + yv[i+1]);
}
*area = *area / 2;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int N, N1, N2;
double *area;
double *xv, *yv ;
N1 = mxGetNumberOfElements(prhs[0]);
N2 = mxGetNumberOfElements(prhs[1]);
if (N1 != N2)
mexErrMsgTxt("Input x and y must have the same length");
else
N = N1;
if (N < 2)
mexErrMsgTxt("The minimum length is 2");
xv = mxGetPr(prhs[0]);
yv = mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
area = mxGetPr(plhs[0]);
trapzfu(area, xv, yv, N);
}
------------ trapzfu2.c --------
/* F=trapzfu2(X,Y) computes the integral of Y with respect to X using
the trapezoidal method. X and Y must be vectors of the same length.
For trapzf to work properly:
X must be a STRICTLY monotonically increasing vector.
X does not need to be evenly spaced.
L. Optican, NEI/NIH/DHHS
*/
#include "mex.h"
void trapzfu2( double *area, double *xv, double *yv, int N) {
register int i, j,r;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int N, N1, N2;
double *area;
double *xv, *yv ;
N1 = mxGetNumberOfElements(prhs[0]);
N2 = mxGetNumberOfElements(prhs[1]);
if (N1 != N2)
mexErrMsgTxt("Input x and y must have the same length");
else
N = N1;
if (N < 2)
mexErrMsgTxt("The minimum length is 2");
xv = mxGetPr(prhs[0]);
yv = mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
area = mxGetPr(plhs[0]);
I tried running my function simultaneously with 3 different datasets together, but the slave processes doesnt do anything and the time is the same as if I am running without it.
Can I use it for running multiple processes accessing the same function using different parameter cell. Please note that the function takes in image input in each call. Will that be a problem ?
Thanks very nice code
Great Package! Definitely useful for multicore CPUs. I have a 6 core AMD. It's such a pain if MATLAB can't run parallel.
I have a demo tip for newbies:
If you want to see the effect, make sure your function have to run at least several times on the input cell. If your function only has one input, all the slave sessions will be doing nothing because the function is already running on master. You'd probably want to make your input into segments to make all the slaves run simultaneously.
The tool is really powerful. I want to know if we can develop a similar tool by Fortran or C language. There are two reasons:
1) since every master and slave program need a matlab window, it is too expensive to buy multiple licenses of Matlab to run on different machines;
2) the slave program is launched by matlab, which will also need more than 100 MB memory, however, maybe the slave program only need to launch another external execute program with different parameters. Write a pure master/pure program will decrease the memory usage greatly.
Thanks,
Zhanhgong Tang
Comment only