-
Notifications
You must be signed in to change notification settings - Fork 5
/
sum_phi.cpp
executable file
·59 lines (48 loc) · 1.71 KB
/
sum_phi.cpp
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
/*********************************************************************
* sum_phi.cpp
* @ Ayan Acharya, Date: Mar 31, 2012
********************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <matrix.h>
#include <mex.h>
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *tmp1, *tmp3, *tmp5, *tmp6;
double *tmp2, *tmp4, *tmp7;
int N; // number of instances
int dimx, dimy;
tmp1 = mxGetField(prhs[0],0,"phi");
N = mxGetNumberOfElements (tmp1);
int nK = (int)mxGetScalar(mxGetField(prhs[0],0,"K"));
plhs[0] = mxCreateNumericMatrix(N, nK, mxDOUBLE_CLASS, 0);
tmp2 = mxGetPr(plhs[0]);
tmp5 = mxGetField(prhs[1],0,"wcount");
for (int i = 0; i < N; i++)
{
tmp3 = mxDuplicateArray (mxGetCell (tmp1, i));
tmp4 = mxGetPr(tmp3);
dimx = (int)mxGetM(tmp3);
dimy = (int)mxGetN(tmp3);
tmp6 = mxDuplicateArray (mxGetCell (tmp5, i));
tmp7 = mxGetPr(tmp6);
int dimxx = (int)mxGetM(tmp6);
int dimxy = (int)mxGetN(tmp6);
// mexPrintf("%d %d %d %d %d\n", i, dimx, dimy, dimxx, dimxy);
// matlab's array index is column wise, so instead of accessing (j,k), access (k,j) when j indexes row and k indexes column
for (int k = 0; k < dimy; k++)
{
double value = 0;
for(int j=0; j<dimx; j++)
{
value+= tmp7[j]*tmp4[k*dimx+j]; //w_{nm'}*phi_{nm'k}
}
tmp2[k*N+i]+= value;
}
}
return;
}