-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtraceAB.c
executable file
·52 lines (44 loc) · 1.28 KB
/
traceAB.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
/*==========================================================
% traceAB.c
% Mex version to compute sum(sum(A.*B')
%
% function tr=traceAB(A,B);;
% INPUT:
% A: NxK matrix
% B : KxN matrix
%
% OUTPUT:
% tr : sum(sum(A.*B')
%
% WARING: for speed reasons the mex version of this function skips all
% checks, they should be implemented in the calling function (mvpattern_covcomp_diag1)
% (c) Naveed Ejaz 2013 [email protected]
*========================================================*/
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// (1) check for proper number of input and output arguments
if(nrhs!=2)
mexErrMsgIdAndTxt("MyToolbox:traceAB_faster:nrhs","2 inputs required.");
int N,K;
int i,j, idx;
double *ptrA, *ptrB;
double tr=0;
// getting dimensions
N = mxGetM(prhs[0]);
K = mxGetN(prhs[0]);
// array pointers
ptrA = mxGetPr(prhs[0]);
ptrB = mxGetPr(prhs[1]);
// simple matrix multiplication
for(i=0; i<N; i++)
{
idx = i*K;
for (j=0; j<K; j++)
tr += ptrA[i+j*N]*ptrB[j+idx];
}
// (5) assigning outputs and returning to matlab
plhs[0] = mxCreateDoubleScalar(tr);
return;
}