-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtsize.c
executable file
·60 lines (51 loc) · 1.71 KB
/
tsize.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
/* tsize.c */
/* S. Engblom 2005-04-10 */
#include <math.h>
#include "mex.h"
#include "matrix.h"
/*-----------------------------------------------------------------------*/
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
/* check of syntax */
if (nrhs != 1 && nrhs != 2 || nlhs > 1)
mexErrMsgIdAndTxt("tsize:e1",
"Expecting one or two inputs and one output.");
/* input */
mwSize ndimA = mxGetNumberOfDimensions(prhs[0]);
const mwSize *sizA = mxGetDimensions(prhs[0]);
/* adjust the number of dimensions */
if (ndimA == 2)
ndimA -= (sizA[1] == 1)*(1+(sizA[0] == 1));
/* full or indexed version */
if (nrhs == 1) {
/* output */
plhs[0] = mxCreateDoubleMatrix(1,ndimA,mxREAL);
double *siz = mxGetPr(plhs[0]);
for (int i = 0; i < ndimA; i++)
siz[i] = (double)sizA[i];
}
else {
if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ||
mxIsSparse(prhs[1]))
mexErrMsgIdAndTxt("tsize:e2",
"Dimension argument must be real, double and non-sparse.");
/* input DIMS */
const mwSize lenDIMS = mxGetNumberOfElements(prhs[1]);
const double *prDIMS = mxGetPr(prhs[1]);
/* check input DIMS */
for (int i = 0; i < lenDIMS; i++)
if (prDIMS[i] < 1.0 || prDIMS[i] != ceil(prDIMS[i]))
mexErrMsgIdAndTxt("tsize:e3",
"Size argument must be nonnegative integers.");
/* output */
plhs[0] = mxCreateDoubleMatrix(1,lenDIMS,mxREAL);
double *siz = mxGetPr(plhs[0]);
for (int i = 0; i < lenDIMS; i++) {
if (prDIMS[i] <= ndimA)
siz[i] = (double)sizA[(int)prDIMS[i]-1];
else
siz[i] = 1.0;
}
}
}
/*-----------------------------------------------------------------------*/