= 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;
};
各位大侠帮忙啊! 不胜赶紧。