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