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