Daniel@0: #include Daniel@0: #include "mex.h" Daniel@0: Daniel@0: /* Daniel@0: out = colop(M, v) Daniel@0: Daniel@0: Apply binary operator to a vector v and to each column of M in turn Daniel@0: to produce a matrix the same size as M. Daniel@0: Daniel@0: This is equivalent to Daniel@0: Daniel@0: out = zeros(size(M)); Daniel@0: for col=1:size(M,2) Daniel@0: out(:,col) = op(M(:,col), v); Daniel@0: end Daniel@0: Daniel@0: The code needs to be modified for each different operator 'op'. Daniel@0: eg op = '.*' Daniel@0: Daniel@0: In vectorized form: Daniel@0: Daniel@0: out = M .* repmat(v(:), 1, size(M,2)) Daniel@0: Daniel@0: (This function was formerly called repmat_and_mult.c) Daniel@0: Daniel@0: */ Daniel@0: Daniel@0: /* M(i,j) = M(i + nrows*j) since Matlab uses Fortran layout. */ Daniel@0: Daniel@0: Daniel@0: #define INMAT(i,j) M[(i)+nrows*(j)] Daniel@0: #define OUTMAT(i,j) out[(i)+nrows*(j)] Daniel@0: Daniel@0: void mexFunction( Daniel@0: int nlhs, mxArray *plhs[], Daniel@0: int nrhs, const mxArray *prhs[] Daniel@0: ) Daniel@0: { Daniel@0: double *out, *M, *v; Daniel@0: int nrows, ncols, r, c; Daniel@0: Daniel@0: /* read the input args */ Daniel@0: M = mxGetPr(prhs[0]); Daniel@0: nrows = mxGetM(prhs[0]); Daniel@0: ncols = mxGetN(prhs[0]); Daniel@0: Daniel@0: v = mxGetPr(prhs[1]); Daniel@0: Daniel@0: plhs[0] = mxCreateDoubleMatrix(nrows, ncols, mxREAL); Daniel@0: out = mxGetPr(plhs[0]); Daniel@0: Daniel@0: for (c=0; c < ncols; c++) { Daniel@0: for (r=0; r < nrows; r++) { Daniel@0: OUTMAT(r,c) = INMAT(r,c) * v[r]; Daniel@0: /* printf("r=%d, c=%d, M=%f, v=%f\n", r, c, INMAT(r,c), v[r]); */ Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: } Daniel@0: Daniel@0: Daniel@0: