-
Notifications
You must be signed in to change notification settings - Fork 14
/
var1mex.c
78 lines (63 loc) · 2.13 KB
/
var1mex.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
69
70
71
72
73
74
75
76
77
78
// Part of the varbvs package, https://github.com/pcarbo/varbvs
//
// Copyright (C) 2012-2017, Peter Carbonetto
//
// This program is free software: you can redistribute it under the
// terms of the GNU General Public License; either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANY; without even the implied warranty of
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// For a description of this C code, see var1.m.
#include "types.h"
#include "misc.h"
#include "doublevectormex.h"
#include "singlematrixmex.h"
#include "mex.h"
#include "matrix.h"
// FUNCTION DECLARATIONS
// -----------------------------------------------------------------
// Compute the sample variance.
double computeVariance (const double* x, Size n);
// FUNCTION DEFINITIONS
// -----------------------------------------------------------------
void mexFunction (int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
// Get the input matrix X.
const SingleMatrix X = getSingleMatrix(prhs[0]);
// Get the number of samples (n) and the number of variables (p).
const Size n = X.nr;
const Size p = X.nc;
// Initialize the output.
DoubleVector y = createMatlabVector(p,&plhs[0]);
// This is storage for a column of matrix X.
double* x = malloc(sizeof(double)*n);
// Repeat for each column of X.
for (Index k = 0; k < p; k++) {
// Get the kth column of X.
copyColumn(X.elems,x,k,n);
// Compute the sample variance.
y.elems[k] = computeVariance(x,n);
}
// Free the dynamically allocated memory.
free(x);
}
// -----------------------------------------------------------------
// Compute the sample variance.
double computeVariance (const double* x, Size n) {
double y = 0; // The return value.
double t; // Intermediate result.
// Compute the sample mean.
double mu = sum(x,n) / n;
// Repeat for each entry of x.
for (Index i = 0; i < n; i++) {
t = x[i] - mu;
y += t*t;
}
// Divide by the number of entries.
y /= n;
return y;
}