如何使用';墨西哥';在Matlab中
我正在尝试运行以下代码:如何使用';墨西哥';在Matlab中,matlab,mex,Matlab,Mex,我正在尝试运行以下代码: mex -O fconv.cc 但我得到了一个错误: 使用mex时出错。找不到支持的编译器或SDK。(链接至 安装了SDK的mathworks SDK没有帮助) 我不是第一次使用Matlab。我在网上找不到任何有用的东西 fconv.cc: #include "mex.h" #include <math.h> #include <string.h> struct thread_data { double *A; double *B;
mex -O fconv.cc
但我得到了一个错误:
使用mex时出错。找不到支持的编译器或SDK。(链接至
安装了SDK的mathworks SDK没有帮助)
我不是第一次使用Matlab。我在网上找不到任何有用的东西
fconv.cc:
#include "mex.h"
#include <math.h>
#include <string.h>
struct thread_data {
double *A;
double *B;
double *C;
mxArray *mxC;
const mwSize *A_dims;
const mwSize *B_dims;
mwSize C_dims[2];
};
// convolve A and B
void *process(void *thread_arg) {
thread_data *args = (thread_data *)thread_arg;
double *A = args->A;
double *B = args->B;
double *C = args->C;
const mwSize *A_dims = args->A_dims;
const mwSize *B_dims = args->B_dims;
const mwSize *C_dims = args->C_dims;
int num_features = args->A_dims[2];
for (int f = 0; f < num_features; f++) {
double *dst = C;
double *A_src = A + f*A_dims[0]*A_dims[1];
double *B_src = B + f*B_dims[0]*B_dims[1];
for (int x = 0; x < C_dims[1]; x++) {
for (int y = 0; y < C_dims[0]; y++) {
double val = 0;
for (int xp = 0; xp < B_dims[1]; xp++) {
double *A_off = A_src + (x+xp)*A_dims[0] + y;
double *B_off = B_src + xp*B_dims[0];
switch(B_dims[0]) {
case 20: val += A_off[19] * B_off[19];
case 19: val += A_off[18] * B_off[18];
case 18: val += A_off[17] * B_off[17];
case 17: val += A_off[16] * B_off[16];
case 16: val += A_off[15] * B_off[15];
case 15: val += A_off[14] * B_off[14];
case 14: val += A_off[13] * B_off[13];
case 13: val += A_off[12] * B_off[12];
case 12: val += A_off[11] * B_off[11];
case 11: val += A_off[10] * B_off[10];
case 10: val += A_off[9] * B_off[9];
case 9: val += A_off[8] * B_off[8];
case 8: val += A_off[7] * B_off[7];
case 7: val += A_off[6] * B_off[6];
case 6: val += A_off[5] * B_off[5];
case 5: val += A_off[4] * B_off[4];
case 4: val += A_off[3] * B_off[3];
case 3: val += A_off[2] * B_off[2];
case 2: val += A_off[1] * B_off[1];
case 1: val += A_off[0] * B_off[0];
break;
default:
for (int yp = 0; yp < B_dims[0]; yp++) {
val += *(A_off++) * *(B_off++);
}
}
}
*(dst++) += val;
}
}
}
return NULL;
}
// matlab entry point
// C = fconv(A, cell of B, start, end);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 4)
mexErrMsgTxt("Wrong number of inputs");
if (nlhs != 1)
mexErrMsgTxt("Wrong number of outputs");
// get A
const mxArray *mxA = prhs[0];
if (mxGetNumberOfDimensions(mxA) != 3 ||
mxGetClassID(mxA) != mxDOUBLE_CLASS)
mexErrMsgTxt("Invalid input: A");
// get B and start/end
const mxArray *cellB = prhs[1];
mwSize num_bs = mxGetNumberOfElements(cellB);
int start = (int)mxGetScalar(prhs[2]) - 1;
int end = (int)mxGetScalar(prhs[3]) - 1;
if (start < 0 || end >= num_bs || start > end)
mexErrMsgTxt("Invalid input: start/end");
int len = end-start+1;
// output cell
plhs[0] = mxCreateCellMatrix(1, len);
// do convolutions
thread_data td;
const mwSize *A_dims = mxGetDimensions(mxA);
double *A = (double *)mxGetPr(mxA);
for (int i = 0; i < len; i++) {
const mxArray *mxB = mxGetCell(cellB, i+start);
td.A_dims = A_dims;
td.A = A;
td.B_dims = mxGetDimensions(mxB);
td.B = (double *)mxGetPr(mxB);
if (mxGetNumberOfDimensions(mxB) != 3 ||
mxGetClassID(mxB) != mxDOUBLE_CLASS ||
td.A_dims[2] != td.B_dims[2])
mexErrMsgTxt("Invalid input: B");
// compute size of output
int height = td.A_dims[0] - td.B_dims[0] + 1;
int width = td.A_dims[1] - td.B_dims[1] + 1;
if (height < 1 || width < 1)
mexErrMsgTxt("Invalid input: B should be smaller than A");
td.C_dims[0] = height;
td.C_dims[1] = width;
td.mxC = mxCreateNumericArray(2, td.C_dims, mxDOUBLE_CLASS, mxREAL);
td.C = (double *)mxGetPr(td.mxC);
process((void *)&td);
mxSetCell(plhs[0], i, td.mxC);
}
}
#包括“mex.h”
#包括
#包括
结构线程数据{
双*A;
双*B;
双*C;
MX阵列*mxC;
const mwSize*A_dims;
常数mwSize*B_dims;
mwSize C_dims[2];
};
//卷积A和B
void*进程(void*线程参数){
线程数据*参数=(线程数据*)线程参数;
双*A=args->A;
双*B=args->B;
双*C=args->C;
常量mwSize*A_dims=args->A_dims;
常量mwSize*B_dims=args->B_dims;
常量mwSize*C_dims=args->C_dims;
int num_features=args->A_dims[2];
对于(int f=0;f=num|bs | |开始>结束)
MEXERMSGSTXT(“无效输入:开始/结束”);
int len=结束-开始+1;
//输出单元
plhs[0]=mxCreateCellMatrix(1,len);
//做卷积
线程_数据td;
常量mwSize*A_dims=mxGetDimensions(mxA);
double*A=(double*)mxGetPr(mxA);
对于(int i=0;i
要使用mex
命令编译mex函数,首先需要确保以下几点:
mex-setup
命令告诉了Matlabmex-setup
命令告诉了Matlab:
执行此命令将引导您完成一个向导,该向导根据常见的安装位置标识已安装的编译器。只需选择要使用的函数。要使用
mex
命令编译mex函数,首先需要确保以下几点:
mex-setup
命令告诉了Matlabmex-setup
命令告诉了Matlab:
执行此命令将引导您完成一个向导,该向导根据常见的安装位置标识已安装的编译器。只需选择要使用的函数。要使用
mex
命令编译mex函数,首先需要确保以下几点:
mex-setup
命令告诉了Matlab