Matrix findWCSMatrix(Matrix imspca, ImageList* srt, int *numberOfClasses, int writeTextInterm) {
    Matrix Sw;
    Matrix classmatrix = duplicateMatrix(imspca);
    ImageList *subject, *replicate;
    int maxClassSize = 0;
    int repCount;

    *numberOfClasses = 0;
    MESSAGE("Finding within-class scatter matrix.");
    for (subject = srt; subject; subject = subject->next_subject) {
        int classStart, classEnd;
        (*numberOfClasses)++;  /* Count the number of classes */

        DEBUG(3, "Finding the class indexes.");
        classStart = classEnd = subject->imageIndex;
        repCount = 0;
        for (replicate = subject; replicate; replicate = replicate->next_replicate) {
            repCount ++;
            classEnd = replicate->imageIndex;
        }

        if (repCount > maxClassSize) { maxClassSize = repCount; }

        if (repCount == 1 && !quiet) {
            printf("WARNING: class %d only contains one image.\n", *numberOfClasses);
        }

        DEBUG_INT(4, "Lower limit on class.", classStart);
        DEBUG_INT(4, "Upper limit on class.", classEnd);
        DEBUG(3, "Mean centering the class.");
        subtractClassMean(classmatrix, classStart, classEnd);

    }

    DEBUG_CHECK(maxClassSize > 1, "ERROR: at least one class must have more than one replicate.  Make sure your image list is not flat.");

    if (debuglevel > 3)
        printMatrix("classmatrix", classmatrix);

    if (writeTextInterm) { SAVE_MATRIX(classmatrix); } /* output textfiles of intermediate matrices */

    DEBUG(3, "Creating the class scatter matrix.");
    Sw = transposeMultiplyMatrixR(classmatrix, classmatrix);

    if (debuglevel > 3)
        printMatrix("Sw", Sw);
    return Sw;
}
Esempio n. 2
0
int resizeRow(matrix * mtx, int Row){

	if(!(*mtx)) return -1;
	if(Row<=0 ) return -1;
	//Find if we are adding or removing from the matrix
	//printf("ResizeRow\n");	
	//If flag equals 1 means decreasing the size of the
	//matrix if it is 0 it means we are increasing the size
	int i;
	int j;
	int cols = (*mtx)->cols;
	int flag = 0;
	int currentRow = 0;
	int rowResize = Row;
	matrix * mtxtemp;
	matrix * mtxprev;
	mtxtemp = mtx;

	if(Row>((*mtx)->rows+currentRow)){
		while((*mtxtemp)->Extra!=NULL && flag==0){
			currentRow+=(*mtxtemp)->rows;
			rowResize=Row-currentRow;
			(*mtxprev) = (*mtxtemp);
			(*mtxtemp)=(*mtxtemp)->Extra;
			if(Row<=((*mtxtemp)->rows+currentRow)){
				flag=1;
			}
		}
	}else{
		flag=1;
	}

	if(flag==0){
		//Increasing
		//If we are increasing mtxtemp->Extra should be NULL
		if((*mtxtemp)->Extra!=NULL){
			printf("ERROR should not be increasing size of matrix\n");
			return -1;
		}

		//Determine if it is the first matrix in the list
		if((*mtx)->Extra==NULL){
			//First matrix in the list
			matrix temp = duplicateMatrix((*mtx));
			matrix mtxnew = newMatrix(rowResize,12);
			
			for(i=1;i<=rowResize;i++){
				for(j=1;j<=cols;j++){
					if(i<=temp->rows){
						setE(mtxnew,i,j,getE(temp,i,j));
					}else{
						setE(mtxnew,i,j,0);
					}
				}
			}

			deleteMatrix(mtx);
			deleteMatrix(&temp);
			(*mtx) = mtxnew;

		}else{
			//Not the first matrix in the list
			matrix temp = duplicateMatrix((*mtxtemp));
			matrix mtxnew = newMatrix(rowResize,12);

			for(i=1;i<=rowResize;i++){
				for(j=1;j<=cols;j++){
					if(i<=temp->rows){
						setE(mtxnew,i,j,getE(temp,i,j));
					}else{
						setE(mtxnew,i,j,0);
					}
				}
			}

			deleteMatrix(mtxtemp);
			deleteMatrix(&temp);
			(*mtxprev)->Extra = mtxnew;

		}

	}else{
		//Decreasing
		//mtxtemp is the matrix that is being reduced in size
		//All matrices linked after mtxtemp are to be deleted
		if((*mtxtemp)->Extra!=NULL){
			matrix mtxRemove = (*mtxtemp)->Extra;
			deleteMatrix(&mtxRemove);
			(*mtxtemp)->Extra=NULL;
		}

		//Now we just need to resize mtxtemp
		matrix temp = duplicateMatrix((*mtxtemp)); 
		matrix mtxnew = (matrix) realloc((*mtxtemp), sizeof(struct _matrix)+sizeof(double)*rowResize*cols);
		if(!mtxnew) {
			printf("ERROR unable to realloc matrix returned NULL\n");
			return -1;
		}
		mtxnew->rows=rowResize;
		mtxnew->cols=cols;
		mtxnew->Extra=NULL;

		for (i=1;i<=rowResize;i++){
			for (j=1;j<=cols;j++){
				if(i<=temp->rows){
					setE(mtxnew,i,j,getE(temp,i,j));
				}else{
					printf("ERROR should not have a value in the new matrix that is greater than the old one\n");
					return -1;
				}
			}
		}

		deleteMatrix(&temp);
		(*mtxtemp) = mtxnew;


	}

	return 0;
}