comparison toolboxes/FullBNT-1.0.7/KPMtools/colmult.c @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 #include <stdio.h>
2 #include "mex.h"
3
4 /*
5 out = colop(M, v)
6
7 Apply binary operator to a vector v and to each column of M in turn
8 to produce a matrix the same size as M.
9
10 This is equivalent to
11
12 out = zeros(size(M));
13 for col=1:size(M,2)
14 out(:,col) = op(M(:,col), v);
15 end
16
17 The code needs to be modified for each different operator 'op'.
18 eg op = '.*'
19
20 In vectorized form:
21
22 out = M .* repmat(v(:), 1, size(M,2))
23
24 (This function was formerly called repmat_and_mult.c)
25
26 */
27
28 /* M(i,j) = M(i + nrows*j) since Matlab uses Fortran layout. */
29
30
31 #define INMAT(i,j) M[(i)+nrows*(j)]
32 #define OUTMAT(i,j) out[(i)+nrows*(j)]
33
34 void mexFunction(
35 int nlhs, mxArray *plhs[],
36 int nrhs, const mxArray *prhs[]
37 )
38 {
39 double *out, *M, *v;
40 int nrows, ncols, r, c;
41
42 /* read the input args */
43 M = mxGetPr(prhs[0]);
44 nrows = mxGetM(prhs[0]);
45 ncols = mxGetN(prhs[0]);
46
47 v = mxGetPr(prhs[1]);
48
49 plhs[0] = mxCreateDoubleMatrix(nrows, ncols, mxREAL);
50 out = mxGetPr(plhs[0]);
51
52 for (c=0; c < ncols; c++) {
53 for (r=0; r < nrows; r++) {
54 OUTMAT(r,c) = INMAT(r,c) * v[r];
55 /* printf("r=%d, c=%d, M=%f, v=%f\n", r, c, INMAT(r,c), v[r]); */
56 }
57 }
58
59 }
60
61
62