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