matlab mex 矩阵,Matlab关于mex编程 返回的矩阵是乱码的问题

= cell(j , i);

}

}

}

double Cell::operator ()(int y , int x) // get the value

{

return matrix[y][x];

}

void Cell::show(int y , int x)

{

if(x==0)

x=width;

if(y==0)

y=height;

for(int indy = 0 ; indy < y ; indy++)

for(int indx = 0 ; indx

{

cout<

";

if(indx == x - 1)

cout<

}

}

void Cell::operator() (int y , int x , double val) //set the value

{

matrix[y][x] = val;

}

void Cell::normalize()

{

for(int indx = 0 ; indxwidth ; indx++)

for(int indy = 0 ; indyheight ; indy++)

{

if(matrix[indy][indx]<0)

matrix[indy][indx] = 0;

else if(matrix[indy][indx]>1)

matrix[indy][indx]=1;

}

}

void Cell::initialize(int y , int x)

{

this->height = y ;

this->width = x;

matrix = new  double *[y];

for(int i=0 ; i

{

matrix= new double[x];

}

for(int y = 0 ; y

for(int x=0 ; x

matrix[y][x] = 0;

}

void Cell::operator -(double val)

{

for(int y = 0 ; ygetHeight() ; y++)

{

for(int x = 0 ; xgetWidth() ; x++)

{

matrix[y][x]  = matrix[y][x] - val;

}

}

}

double mean(Cell cell)

{

double meanval = 0;

for(int y = 0 ; y

{

for(int x = 0 ; x

{

meanval += cell(y,x);

}

}

return meanval/(cell.getHeight()*cell.getWidth());

}

/*

* Matlab interface.

*/

Cell * toCell(const mxArray * a)

{

int mrows = static_cast(mxGetM(a));

int ncols = static_cast(mxGetN(a));

double *data = mxGetPr(a);

Cell * m = new Cell(mrows, ncols);

for (int r = 0; r < mrows; r++) {

for (int c = 0; c < ncols; c++) {

(*m)(r,c,data[(c*mrows) + r]);

}

}

return m;

}

/*

* Convert a 2D matrix to an mxArray.

*/

mxArray* to_mxArray(Cell m)

{

int mrows = m.getHeight();

int ncols = m.getWidth();

mxArray *a = mxCreateDoubleMatrix(

static_cast(mrows),

static_cast(ncols),

mxREAL

);

double *data = mxGetPr(a);

for (unsigned long r = 0; r < mrows; r++) {

for (unsigned long c = 0; c < ncols; c++) {

data[(c*mrows) + r] = m(r,c);

}

}

return a;

}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

{

Cell m = toCell(prhs[0]);

mxArray * ss = to_mxArray(m);

plhs[0]= ss;

}

Cell.h 的如下:

class Cell //storage for the filter, column first

{

public:

Cell(); // constrution, do nothing.

Cell( int y , int x ); // empty matrix with height ==y and width ==x;

Cell ( Cell & cell); // deep copy

double operator () ( int y , int x ); // return the value at (y,x);

void initialize(int y , int x);

void operator () (int y , int x , double val); // set the value at (y , x);

void operator - (double val); //sub the val for each value of the matrix

void normalize(); // set to 1 if val > 1 and to 0 if val < 0

void show(int y=0 , int x=0); //show all elements in the matrix

friend double mean(Cell cell);// get the mean value of the matrix

int getHeight()

{

return height;

}

int getWidth()

{

return width;

}

~Cell(){};

//void setValue(int y , int x , double val);// set the value at (y,x) with val;

private:

double ** matrix;

int height;

int width;

};

各位大侠帮忙啊! 不胜赶紧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值