u013397974 于 2014.02.28 16:55 提问

MatrixXd tempY=mScores;
//int size=tempY.size();
double *Y=new double[nsmp];
double *Max=new double[nsmp];
double max=0.0;
double *min=new double[nsmp];
for(int i=0;i {
min[i]=tempY(0,i);
}
for(int i=0;i {
max=min[i];
for(int j=0;j {
if(tempY(j,i)>max)
{
max=tempY(j,i);
}
}
Max[i]=max;
}
for(int i=0;i<tempY.cols();i++)
{
for(int j=0;j<tempY.rows();j++)
{
if(tempY(j,i)==Max[i])
{
tempY(j,i)=1;
}
else
{
tempY(j,i)=0;
}
}
}
cout<<"the sample is belong to ";
for(int i=0;i<tempY.cols();i++)
{
for(int j=0;j<tempY.rows();j++)
{
if(tempY(j,i)==1)
{
Y[i]=j+1;
cout<<Y[i]<<" ";
}
}
}
cout<<endl;
outY=Y;
return 0;
}

int compare( const void *a, const void *b )
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}

// builds 1-of-K target matrix from labels array
//template
MatrixXd buildTargetMatrix( double *Y, int nLabels ) {

``````// make a temporary copy of the labels array
double *tmpY = new double[ nLabels ];
for ( int i = 0 ; i < nLabels ; i++ ) {
tmpY[i] = Y[i];
}

// sort the array of labels
qsort( tmpY, nLabels, sizeof(double), compare );

// count unique labels
int nunique = 1;
for ( int i = 0 ; i < nLabels - 1 ; i++ ) {
if ( tmpY[i] != tmpY[i+1] )
nunique++;
}

delete [] tmpY;

MatrixXd targets( nunique, nLabels );
targets.fill( 0 );

// fill in the ones
for ( int i = 0 ; i < nLabels ; i++ )
{
int idx = Y[i]-1;
targets( idx, i ) = 1;
}

// normalize the targets matrix values (-1/1)
//targets *= 2;
//targets.array() -= 1;

return targets;
``````

}

#endif