コード例 #1
0
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  {
  // Check the number of input arguments.
  if (nrhs != 2)
    mexErrMsgTxt("Incorrect number of input arguments.");
  
  // Check type of input.
  if ( (mxGetClassID(prhs[0]) != mxDOUBLE_CLASS) || (mxGetClassID(prhs[1]) != mxDOUBLE_CLASS) )
    mexErrMsgTxt("Input must me of type double.");
  
  // Check if input is real.
  if ( (mxIsComplex(prhs[0])) || (mxIsComplex(prhs[1])) )
    mexErrMsgTxt("Input must be real.");
  
  // Create matrices X and Y from the first and second argument.
  mat X = armaGetPr(prhs[0]);
  mat Y = armaGetPr(prhs[1]);
  
  // Our calculations require that matrices must be of the same size 
  if ( size(X) != size(Y) )
    mexErrMsgTxt("Matrices should be of same size.");
  
  // Perform calculations
  mat A = X + Y;
  mat B = X % Y;  // % means element-wise multiplication in Armadillo
  
  // Create cube C with A and B as slices.
  cube C(A.n_rows, A.n_cols, 2);
  
  C.slice(0) = A;
  C.slice(1) = B;
  
  // Create the output argument plhs[0] to return cube C
  plhs[0] = armaCreateMxMatrix(C.n_rows, C.n_cols, C.n_slices);
  
  // Return the cube C as plhs[0] in Matlab/Octave
  armaSetCubePr(plhs[0], C);
  
  return;
  }
コード例 #2
0
ファイル: kernelCore_mex.cpp プロジェクト: liaohaofu/cslLibs
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    if (nrhs != 3)
    {
        mexErrMsgTxt("kernelCore: not enough parameters!");
    }
    
    if (!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
    {
        mexErrMsgTxt("kernelCore: Invalid parameters!");
    }
    
    mat x = armaGetPr(prhs[0]);
    cube scale = armaGetCubePr(prhs[1]);
    Kernel *kernel = convertMat2Ptr<Kernel>(mxGetProperty(prhs[2], 0, "objectHandle"));
    
    if (nlhs == 1)
    {
        vec f;
        
        if (THREAD_NUMBER > x.n_rows)
        {
            KernelModules::kernelCore(f, x, scale, *kernel);
        }
        else
        {
            KernelModules::kernelCore_fast_parallel(f, x, scale, *kernel);
        }
        
        if (plhs[0] == NULL)
        {
            plhs[0] = mxCreateDoubleMatrix(f.n_rows, f.n_cols, mxREAL);
        }
        armaSetPr(plhs[0], f);
    }
    
    if (nlhs == 2)
    {
        vec f;
        mat g;
        
        if (THREAD_NUMBER > x.n_rows)
        {
            KernelModules::kernelCore(f, g, x, scale, *kernel);
        }
        else
        {
            KernelModules::kernelCore_fast_parallel(f, g, x, scale, *kernel);
        }
        
        if (plhs[0] == NULL)
        {
            plhs[0] = mxCreateDoubleMatrix(f.n_rows, f.n_cols, mxREAL);
        }
        if (plhs[1] == NULL)
        {
            plhs[1] = mxCreateDoubleMatrix(g.n_rows, g.n_cols, mxREAL);
        }
        armaSetPr(plhs[0], f);
        armaSetPr(plhs[1], g);
    }
    
    if (nlhs == 3)
    {
        vec f;
        mat g;
        cube H;
        
        if (THREAD_NUMBER > x.n_rows)
        {
            KernelModules::kernelCore(f, g, H, x, scale, *kernel);
        }
        else
        {
            KernelModules::kernelCore_fast_parallel(f, g, H, x, scale, *kernel);
        }
        
        if (plhs[0] == NULL)
        {
            plhs[0] = mxCreateDoubleMatrix(f.n_rows, f.n_cols, mxREAL);
        }
        if (plhs[1] == NULL)
        {
            plhs[1] = mxCreateDoubleMatrix(g.n_rows, g.n_cols, mxREAL);
        }
        if (plhs[2] == NULL)
        {
            mwSize dim[3];
            dim[0] = H.n_rows;
            dim[1] = H.n_cols;
            dim[2] = H.n_slices;
            plhs[2] = mxCreateNumericArray(3, dim, mxDOUBLE_CLASS, mxREAL);
        }
        armaSetPr(plhs[0], f);
        armaSetPr(plhs[1], g);
        armaSetCubePr(plhs[2], H);
    }
}