Ejemplo n.º 1
0
//////////////////////////////////////////////////////////////////////////////////////////////////////
// function [affinityTab, AsymAffTab] = gdlInitAffinityTable_c (IminuszW, initClusters)
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, mxArray *prhs[]) 
{
    mxArray *graphW = prhs[0];
    mxArray *initClusters = prhs[1];

    if (nrhs != 2) {
        mexErrMsgTxt("Wrong number of input!");
    }
    if (mxGetNumberOfDimensions(graphW) != 2 || mxGetM(graphW) != mxGetN(graphW)) {
		mexErrMsgTxt("graphW is not a square matrix!");
	}
    double *pW = mxGetPr(graphW);
    const int height = mxGetM(graphW);
 
    if (!mxIsCell(initClusters)) {
		mexErrMsgTxt("initClusters is not a cell!");
    }
    int numClusters = mxGetNumberOfElements (initClusters);
    
    // output:
	plhs[0] = mxCreateDoubleMatrix(numClusters,numClusters,mxREAL);
    mxArray *affinityTab = plhs[0]; // reference
    double *affinityTabEntry = mxGetPr(affinityTab);
    for (int i = 0; i < numClusters*numClusters; i++) { affinityTabEntry[i] = - MYINF; }
	plhs[1] = mxCreateDoubleMatrix(numClusters,numClusters,mxREAL);
    mxArray *AsymAffTab = plhs[1]; // reference
    double *AsymAffTabEntry = mxGetPr(AsymAffTab);
    for (int i = 0; i < numClusters*numClusters; i++) { AsymAffTabEntry[i] = - MYINF; }
    
    // computing
    double tmpAsymAff[2];
    double *pTable_j = affinityTabEntry;
    for (int j = 0; j < numClusters; j++) {
        mxArray *cluster_j = mxGetCell (initClusters, j);  // cluster j
        for (int i = 0; i < j; i++) {
            pTable_j[i] = gdlComputeAffinity(pW, height, mxGetCell (initClusters, i), cluster_j, tmpAsymAff);  // affinityTabEntry[i+j*numClusters]
            AsymAffTabEntry[i+j*numClusters] = tmpAsymAff[0];
            AsymAffTabEntry[j+i*numClusters] = tmpAsymAff[1];
        }
        pTable_j += numClusters;
    }
   
}
Ejemplo n.º 2
0
//////////////////////////////////////////////////////////////////////////////////////////////////////
// function [L, L_i, L_j] =  = gdlAffinity_c (graphW, cluster_i, cluster_j)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{
    const mxArray *graphW = prhs[0];
    const mxArray *cluster_i = prhs[1];
    const mxArray *cluster_j = prhs[2];

    const int height = mxGetM(graphW);  // height of a matlab matrix
    if (mxGetNumberOfDimensions(graphW) != 2 || height != mxGetN(graphW)) {
		mexErrMsgTxt("graphW is not a square matrix!");
	}
    double *pW = mxGetPr(graphW);
 
    // output:
    double AsymAff[2];
	plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
    *mxGetPr(plhs[0]) = gdlComputeAffinity (pW, height, cluster_i, cluster_j, AsymAff);
	plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
	plhs[2] = mxCreateDoubleMatrix(1,1,mxREAL);
    *mxGetPr(plhs[1]) = AsymAff[0];
    *mxGetPr(plhs[2]) = AsymAff[1];
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// function [affinityTab, AsymAffTab] = gdlInitAffinityTable_knn_c (graphW, initClusters, Kc)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]) 
{
    mxArray *graphW = prhs[0];
    mxArray *initClusters = prhs[1];
    mxArray *Kc = prhs[2];

    if (nrhs != 3) {
        mexErrMsgTxt("Wrong number of input!");
    }
    if (mxGetNumberOfDimensions(graphW) != 2 || mxGetM(graphW) != mxGetN(graphW)) {
		mexErrMsgTxt("graphW is not a square matrix!");
	}
    double *pW = mxGetPr(graphW);
    const int height = mxGetM(graphW);
 
    if (!mxIsCell(initClusters)) {
		mexErrMsgTxt("initClusters is not a cell!");
    }
    int numClusters = mxGetNumberOfElements (initClusters);
    
    // output:
	plhs[0] = mxCreateDoubleMatrix(numClusters,numClusters,mxREAL);
    mxArray *affinityTab = plhs[0]; // reference
    double *affinityTabEntry = mxGetPr(affinityTab);
    for (int i = 0; i < numClusters*numClusters; i++) { affinityTabEntry[i] = - MYINF; }
	plhs[1] = mxCreateDoubleMatrix(numClusters,numClusters,mxREAL);
    mxArray *AsymAffTab = plhs[1]; // reference
    double *AsymAffTabEntry = mxGetPr(AsymAffTab);
    for (int i = 0; i < numClusters*numClusters; i++) { AsymAffTabEntry[i] = - MYINF; }
    
    // computing approximation
    double *pTable_j = affinityTabEntry;
    for (int j = 0; j < numClusters; j++) {
        mxArray *cluster_j = mxGetCell (initClusters, j);  // cluster j
        for (int i = 0; i < j; i++) {  // note: pass negative affinity
            pTable_j[i] = - computeAverageDegreeAffinity (pW, height, mxGetCell (initClusters, i), cluster_j);  // affinityTabEntry[i+j*numClusters]
        }
        pTable_j[j] = - MYINF;
        pTable_j += numClusters;
    }
    // from upper triangular to full symmetric
    // affinityTab = triu(affinityTab) + triu(affinityTab)';
    pTable_j = affinityTabEntry;
    for (int j = 0; j < numClusters; j++) {
        for (int i = j+1; i < numClusters; i++) {  // note: pass negative affinity
            pTable_j[i] = affinityTabEntry[j+i*numClusters];  // affinityTabEntry[i+j*numClusters]
        }
        pTable_j += numClusters;
    }
    // sort
    mxArray *subplhs[1] = {NULL};
    mxArray *subprhs[2] = {affinityTab, Kc};
    mxArray *inKcCluster = NULL;
    bool *pInKcCluster = NULL;
    if (mexCallMATLAB(1, subplhs, 2, subprhs, "gacFindKcCluster") == 0) {
        inKcCluster = subplhs[0];
        pInKcCluster = mxGetLogicals(inKcCluster);
    }

    // computing
    double tmpAsymAff[2];
    pTable_j = affinityTabEntry;
    bool *pInKcCluster_j = pInKcCluster;
    for (int j = 0; j < numClusters; j++) {
        mxArray *cluster_j = mxGetCell (initClusters, j);  // cluster j
        for (int i = 0; i < j; i++) {
            if (pInKcCluster_j[i]) {
                pTable_j[i] = gdlComputeAffinity(pW, height, mxGetCell (initClusters, i), cluster_j, tmpAsymAff, 1);  // affinityTabEntry[i+j*numClusters]
                AsymAffTabEntry[i+j*numClusters] = tmpAsymAff[0];
                AsymAffTabEntry[j+i*numClusters] = tmpAsymAff[1];
            }
            else {
                pTable_j[i] = - MYINF;  // affinityTabEntry[i+j*numClusters]
            }
        }
        pTable_j += numClusters;
        pInKcCluster_j += numClusters;
    }
    // from upper triangular to full symmetric
    // affinityTab = triu(affinityTab) + triu(affinityTab)';
    pTable_j = affinityTabEntry;
    for (int j = 0; j < numClusters; j++) {
        for (int i = j+1; i < numClusters; i++) {  // note: pass negative affinity
            pTable_j[i] = affinityTabEntry[j+i*numClusters];  // affinityTabEntry[i+j*numClusters]
        }
        pTable_j += numClusters;
    }
    
    mxDestroyArray(inKcCluster);
}