-
Notifications
You must be signed in to change notification settings - Fork 13
/
avg.c
42 lines (32 loc) · 906 Bytes
/
avg.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
/* avg.c: MEX file that calculates mean
*
* a = avg(x) caculates the mean of vector x
*
*/
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double sum;
double *x, *avg;
mwSize mrows,ncols;
int i;
/* Check for proper number of arguments. */
//if(nrhs!=1) {
// mexErrMsgTxt("One input required.");
//} else if(nlhs>1) {
// mexErrMsgTxt("Too many output arguments");
//}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
avg = mxGetPr(plhs[0]);
sum=0;
for(i=0; i<=mrows-1; i++)
sum += x[i];
avg[0] = sum/mrows;
}