簡體   English   中英

使用C ++ mex函數從matlab獲取輸入參數

[英]getting input parameters from matlab using C++ mex Function

double learning_rate = 1;
int training_epochs = 1;
int k = 1;

int train_S = 6;
int test_S = 6;
int visible_E = 6;
int hidden_E = 6;

// training data
int train_X[6][6] = {
    {1, 1, 1, 0, 0, 0},
    {1, 0, 1, 0, 0, 0},
    {1, 1, 1, 0, 0, 0},
    {0, 0, 1, 1, 1, 0},
    {0, 0, 1, 1, 1, 0},
    {0, 0, 1, 1, 1, 0}
};

上面的代碼是我給函數的輸入參數。 但是我想將它們與我的mexFunction一起轉換為一個函數,並簡單地調用它們。 Matlab端具有以下內容

clear *
close all
clc

%% Load the data


X=    [ 1, 1, 1, 0, 0, 0; ...
        1, 0, 1, 0, 0, 0; ...
        1, 1, 1, 0, 0, 0; ...
        0, 0, 1, 1, 1, 0; ...
        0, 0, 1, 1, 1, 0; ...
        0, 0, 1, 1, 1, 0];

%% Define Parameters

numHiddenUnits = 6;
numIterations = 1000;
kCD = 1;

%% Compute the RBM

x = RBM(X, numHiddenUnits, numIterations, kCD);

標量輸入參數非常簡單。 矩陣輸入有點麻煩,因為它們使用老式的Fortran列主要順序,因此在將數據發送到函數之前,可能需要轉置數據。 這是一個示例,您必須填寫空白:

/*=========================================================
 * Built on:
 * matrixDivide.c - Example for illustrating how to use 
 * LAPACK within a C MEX-file.
 *
 * This is a MEX-file for MATLAB.
 * Copyright 2009 The MathWorks, Inc.
 *=======================================================*/
/* $Revision: 1.1.6.2 $ $Date: 2009/05/18 19:50:18 $ */

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double * pX, * pNumHiddenUnits, * pNumIter, * pkCD; /* pointers to inputs */
    double * pOutput;  /* output arugments */
    mwSignedIndex m,n;     /* matrix dimensions */ 
    int i, j;

      /* Check for proper number of arguments. */
    if ( nrhs != 4)
    {
        mexErrMsgIdAndTxt("MATLAB:RBM:rhs",
            "This function requires 4 inputs.");
    }

    pX = mxGetPr(prhs[0]); /* pointer to first input, X matrix */
    pNumHiddenUnits = mxGetPr(prhs[1]); /* pointer to second input, scalar hidden units */
    pNumIter = mxGetPr(prhs[2]); /* pointer to third input, scalar number of iterations */
    pkCD = mxGetPr(prhs[3]); /* pointer to third input, scalar kCD */

    /* dimensions of input matrix */
    m = (mwSignedIndex)mxGetM(prhs[0]);  
    n = (mwSignedIndex)mxGetN(prhs[0]);

    /* Validate input arguments */
    if (m < 1 && n < 1)
    {
        mexErrMsgIdAndTxt("MATLAB:RBM:notamatrix",
            "X must be a matrix.");
    }

    plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
    pOutput = mxGetPr(plhs[0]);

    for (i = 0; i < n; ++i)
    {
      for (j = 0; j < m; ++j)
      {
         int index = j * n + i;
         pOutput[index] = pX[i * m + j];
      }
    }
  }
  /* */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM