Skip to content

Instantly share code, notes, and snippets.

@mathsam
Created July 8, 2013 03:40
Show Gist options
  • Save mathsam/5946076 to your computer and use it in GitHub Desktop.
Save mathsam/5946076 to your computer and use it in GitHub Desktop.
It is actually doing a specific batch linear interpolation job for a 3D array.
/* nakeinterp3.c, use specifically for calIsenFlux */
/* interpolate a 3d array A(i,j,k) only in k direction */
/* interface as PT3dInterp = nakeinterp3(numx,numy,z,PT3d,zi) */
/* numx, numy are the first two dimensions */
/* z is the coordinate, zi is the interoplated coordinate */
/* PT3d is the function to be interoplated, only done so in z */
/* z and zi should be column vector */
#include "mex.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
double *PT3d, *PT3dInterp;
double *z, *zi;
mwSize numx, numy, numz, numzi;
mwSize dims[3] = {0};
mwSize nx, m, k, i1, i9, imid, baseindex, baseArea;
mwSize loopX, loopY;
double zik;
numx = (int) * mxGetPr(prhs[0]);
numy = (int) * mxGetPr(prhs[1]);
z = mxGetPr(prhs[2]);
PT3d = mxGetPr(prhs[3]);
zi = mxGetPr(prhs[4]);
numz = (int) mxGetM(prhs[2]);
numzi= (int) mxGetM(prhs[4]);
dims[0] = numx;
dims[1] = numy;
dims[2] = numzi;
plhs[0] = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL);
PT3dInterp = mxGetPr(plhs[0]);
baseArea = numx*numy;
for (k=numzi; k--;)
{
zik = zi[k];
i1 = 0;
i9 = numz-1;
while (i9>i1+1)
{
imid = (i1+i9+1)/2;
if (z[imid] < zik) i1 = imid;
else i9 = imid;
}
if (i1 == i9) {
for (loopX=0;loopX<numx;loopX++)
{
for (loopY=0;loopY<numy;loopY++)
{
baseindex = loopX+numx*loopY;
PT3dInterp[baseindex+baseArea*k] =
PT3d[baseindex+baseArea*i1];
}
}
}
else
{
for (loopX=0;loopX<numx;loopX++)
{
for (loopY=0;loopY<numy;loopY++)
{
baseindex = loopX+numx*loopY;
PT3dInterp[baseindex+baseArea*k] =
PT3d[baseindex+baseArea*i1] +
(PT3d[baseindex+baseArea*i9]-PT3d[baseindex+baseArea*i1]) *
(zik-z[i1]) / (z[i9]-z[i1]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment