forked from mtex-toolbox/mtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S1Grid_find.c
68 lines (49 loc) · 1.32 KB
/
S1Grid_find.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* find x in y*/
/* If you are using a compiler that equates NaN to be zero, you must
* compile this example using the flag -DNAN_EQUALS_ZERO. For example:
*
* mex -DNAN_EQUALS_ZERO fulltosparse.c
*
* This will correctly define the IsNonZero macro for your C compiler.
*/
#if defined(NAN_EQUALS_ZERO)
#define IsNonZero(d) ((d)!=0.0 || mxIsNaN(d))
#else
#define IsNonZero(d) ((d)!=0.0)
#endif
#include "S1Grid.c"
/* Input Arguments */
#define Y_IN prhs[0]
#define MIN_IN prhs[1]
#define P_IN prhs[2]
#define X_IN prhs[3]
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray*prhs[] )
{
S1Grid S1G;
double *x;
mwSize nx;
int ix;
double *ind;
/* Check for proper number of arguments */
if (nrhs != 4) {
mexErrMsgTxt("Four input arguments required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
/* get input */
nx = mxGetM(X_IN) * mxGetN(X_IN);
S1Grid_init(&S1G,mxGetPr(Y_IN),
mxGetM(Y_IN) * mxGetN(Y_IN),
*(double*) mxGetPr(MIN_IN),
*(double*) mxGetPr(P_IN));
x = mxGetPr(X_IN);
/* generate output matrix */
plhs[0] = mxCreateDoubleMatrix(nx,1,0);
ind = mxGetPr(plhs[0]);
/* find */
for (ix=0;ix<nx;ix++)
ind[ix] = 1 + S1Grid_find(&S1G,x[ix]);
/* free memory */
S1Grid_finalize(&S1G);
}