source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/lightspeed/sample_hist.c @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 1.3 KB
Line 
1/* compile with: cmex sample_hist.c util.o -lm
2 *
3 * Each column of p is a distribution which is sampled from n times.
4 * Requires all(col_sum(p) == 1).
5 */
6#include "mex.h"
7#include "util.h"
8
9void MultiRand(double *p, int len, int n, double *result)
10{
11  double z = 1;
12  int i;
13  for(i=1;i<len;i++) {
14    int r = BinoRand(*p/z, n);
15    *result++ = r;
16    n -= r;
17    z -= *p;
18    if(z == 0) {
19      memset(result, 0, (len-i)*sizeof(double));
20      return;
21    }
22    p++;
23  }
24  *result = n;
25}
26
27void mexFunction(int nlhs, mxArray *plhs[],
28                 int nrhs, const mxArray *prhs[])
29{
30  int rows, cols, n, i;
31  double *p, *r;
32
33  if(nrhs != 2)
34    mexErrMsgTxt("Usage: h = sample_hist(p, n)");
35
36  /* prhs[0] is first argument.
37   * mxGetPr returns double*  (data, col-major)
38   * mxGetM returns int  (rows)
39   * mxGetN returns int  (cols)
40   */
41  rows = mxGetM(prhs[0]);
42  cols = mxGetN(prhs[0]);
43  p = mxGetPr(prhs[0]);
44  n = (int)*mxGetPr(prhs[1]);
45  if(mxGetM(prhs[1]) != 1 || mxGetN(prhs[1]) != 1)
46    mexErrMsgTxt("n must be scalar");
47
48  if(mxIsSparse(prhs[0]))
49    mexErrMsgTxt("Cannot handle sparse matrices.  Sorry.");
50
51  /* plhs[0] is first output */
52  plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL);
53  r = mxGetPr(plhs[0]);
54  for(i=0;i<cols;i++) {
55    MultiRand(p, rows, n, r);
56    p += rows;
57    r += rows;
58  }
59}
60
Note: See TracBrowser for help on using the repository browser.