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