range2.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. [MIN, MAX] = range2(MTX)
  3. >>> See range2.m for documentation <<<
  4. EPS, 3/97.
  5. */
  6. #define V4_COMPAT
  7. #include <matrix.h> /* Matlab matrices */
  8. #include <mex.h>
  9. #include <stddef.h> /* NULL */
  10. #define notDblMtx(it) (!mxIsNumeric(it) || !mxIsDouble(it) || mxIsSparse(it) || mxIsComplex(it))
  11. void mexFunction(int nlhs, /* Num return vals on lhs */
  12. mxArray *plhs[], /* Matrices on lhs */
  13. int nrhs, /* Num args on rhs */
  14. const mxArray *prhs[] /* Matrices on rhs */
  15. )
  16. {
  17. register double temp, mn, mx;
  18. register double *mtx;
  19. register int i, size;
  20. const mxArray *arg;
  21. if (nrhs != 1) mexErrMsgTxt("requires 1 argument.");
  22. /* ARG 1: MATRIX */
  23. arg = prhs[0];
  24. if notDblMtx(arg) mexErrMsgTxt("MTX arg must be a real non-sparse matrix.");
  25. mtx = mxGetPr(arg);
  26. size = (int) mxGetM(arg) * mxGetN(arg);
  27. /* FIND min, max values of MTX */
  28. mn = *mtx; mx = *mtx;
  29. for (i=1; i<size; i++)
  30. {
  31. temp = mtx[i];
  32. if (temp < mn)
  33. mn = temp;
  34. else if (temp > mx)
  35. mx = temp;
  36. }
  37. plhs[0] = (mxArray *) mxCreateDoubleMatrix(1,1,mxREAL);
  38. if (plhs[0] == NULL) mexErrMsgTxt("Error allocating result matrix");
  39. plhs[1] = (mxArray *) mxCreateDoubleMatrix(1,1,mxREAL);
  40. if (plhs[1] == NULL) mexErrMsgTxt("Error allocating result matrix");
  41. mtx = mxGetPr(plhs[0]);
  42. mtx[0] = mn;
  43. mtx = mxGetPr(plhs[1]);
  44. mtx[0] = mx;
  45. return;
  46. }