数据结构:压缩矩阵的转置

数据结构:压缩矩阵的转置,第1张

摘要:在平常的二位数组运用中,总是容易造成空间浪费,所以将矩阵压缩很有必要。

一、压缩矩阵转置 1、定义

压缩矩阵存放原矩阵的行数、列数和有效数据的个数。

//定义的一个矩阵 
typedef struct Triple{
    int i;
    int j;
    elem e;
} Triple, *TriplePtr;

//压缩后的矩阵 
typedef struct CompressedMatrix{
    int rows,columns,numElements;
    Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;
2、初始化

初始化是先动态分配一块动态的空间,然后将压缩后的矩阵传入并赋值。

CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData){
	int i;
	CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
	resultPtr->rows = paraRows;
	resultPtr->columns = paraColumns;
	resultPtr->numElements = paraElements;
	resultPtr->elements = (TriplePtr)malloc(paraElements * sizeof(struct Triple));

	for(i = 0; i < paraElements; i ++){
		resultPtr->elements[i].i = paraData[i][0];
		resultPtr->elements[i].j = paraData[i][1];
		resultPtr->elements[i].e = paraData[i][2];
	}//Of for i

	return resultPtr;
}// Of initCompressedMatrix
3、打印
void printCompressedMatrix(CompressedMatrixPtr paraPtr){
	int i;
	for(i = 0; i < paraPtr->numElements; i ++){
		printf("(%d, %d): %d\r\n", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
	}//Of for i
}// Of printCompressedMatrix
4、压缩矩阵的转置

所谓矩阵的转置,就是把矩阵的行数转变为列数、把矩阵的列数转变为行数,但是数据元素不加改变。

CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr){
	//Step 1. Allocate space.
	int i, tempColumn, tempPosition;
	int *tempColumnCounts = (int*)malloc(paraPtr->columns * sizeof(int));
	int *tempOffsets = (int*)malloc(paraPtr->columns * sizeof(int));
	for(i = 0; i < paraPtr->columns; i ++){
		tempColumnCounts[i] = 0;
	}//Of for i

	CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
	resultPtr->rows = paraPtr->columns;
	resultPtr->columns = paraPtr->rows;
	resultPtr->numElements = paraPtr->numElements;

	resultPtr->elements = (TriplePtr)malloc(paraPtr->numElements * sizeof(struct Triple));
	
	//Step 2. One scan to calculate offsets.
	for(i = 0; i < paraPtr->numElements; i ++) {
		tempColumnCounts[paraPtr->elements[i].j] ++;
	}//Of for i
	tempOffsets[0] = 0;
	for(i = 1; i < paraPtr->columns; i ++){
		tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
		printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
	}//Of for i

	//Step 3. Another scan to fill data.
	for(i = 0; i < paraPtr->numElements; i ++) {
		tempColumn = paraPtr->elements[i].j;
		tempPosition = tempOffsets[tempColumn];
		resultPtr->elements[tempPosition].i = paraPtr->elements[i].j;
		resultPtr->elements[tempPosition].j = paraPtr->elements[i].i;
		resultPtr->elements[tempPosition].e = paraPtr->elements[i].e;

		tempOffsets[tempColumn]++;
	}//Of for i

	return resultPtr;
}//Of transposeCompressedMatrix
5、测试函数
void compressedMatrixTest(){
	CompressedMatrixPtr tempPtr1, tempPtr2;
	int i, j, tempElements;

	//Construct the first sample matrix.
	tempElements = 4;
	int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
	for(i = 0; i < tempElements; i ++){
		tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
	}//Of for i

	int tempMatrix2[4][3] = {{0, 0, 2}, {0, 2, 3}, {2, 0, 5}, {2, 1, 6}};
	for(i = 0; i < tempElements; i ++){
		for(j = 0; j < 3; j ++) {
			tempMatrix1[i][j] = tempMatrix2[i][j];
		}//Of for j
	}//Of for i
	
	tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);

	printf("After initialization.\r\n");
	printCompressedMatrix(tempPtr1);

	tempPtr2 = transposeCompressedMatrix(tempPtr1);
	printf("After transpose.\r\n");
	printCompressedMatrix(tempPtr2);
}// Of main
6、完整代码
#include 
#include 

typedef int elem;

//定义的一个矩阵 
typedef struct Triple{
    int i;
    int j;
    elem e;
} Triple, *TriplePtr;

//压缩后的矩阵 
typedef struct CompressedMatrix{
    int rows,columns,numElements;
    Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;

/**
 * 初始化一个压缩后的矩阵 
 */
CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData){
	int i;
	CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
	resultPtr->rows = paraRows;
	resultPtr->columns = paraColumns;
	resultPtr->numElements = paraElements;
	resultPtr->elements = (TriplePtr)malloc(paraElements * sizeof(struct Triple));

	for(i = 0; i < paraElements; i ++){
		resultPtr->elements[i].i = paraData[i][0];
		resultPtr->elements[i].j = paraData[i][1];
		resultPtr->elements[i].e = paraData[i][2];
	}//Of for i

	return resultPtr;
}// Of initCompressedMatrix

/**
 * Print the compressed matrix.
 */
void printCompressedMatrix(CompressedMatrixPtr paraPtr){
	int i;
	for(i = 0; i < paraPtr->numElements; i ++){
		printf("(%d, %d): %d\r\n", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
	}//Of for i
}// Of printCompressedMatrix

/**
 * Transpose a compressed matrix.
 */
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr){
	//Step 1. Allocate space.
	int i, tempColumn, tempPosition;
	int *tempColumnCounts = (int*)malloc(paraPtr->columns * sizeof(int));
	int *tempOffsets = (int*)malloc(paraPtr->columns * sizeof(int));
	for(i = 0; i < paraPtr->columns; i ++){
		tempColumnCounts[i] = 0;
	}//Of for i

	CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
	resultPtr->rows = paraPtr->columns;
	resultPtr->columns = paraPtr->rows;
	resultPtr->numElements = paraPtr->numElements;

	resultPtr->elements = (TriplePtr)malloc(paraPtr->numElements * sizeof(struct Triple));
	
	//Step 2. One scan to calculate offsets.
	for(i = 0; i < paraPtr->numElements; i ++) {
		tempColumnCounts[paraPtr->elements[i].j] ++;
	}//Of for i
	tempOffsets[0] = 0;
	for(i = 1; i < paraPtr->columns; i ++){
		tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
		printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
	}//Of for i

	//Step 3. Another scan to fill data.
	for(i = 0; i < paraPtr->numElements; i ++) {
		tempColumn = paraPtr->elements[i].j;
		tempPosition = tempOffsets[tempColumn];
		resultPtr->elements[tempPosition].i = paraPtr->elements[i].j;
		resultPtr->elements[tempPosition].j = paraPtr->elements[i].i;
		resultPtr->elements[tempPosition].e = paraPtr->elements[i].e;

		tempOffsets[tempColumn]++;
	}//Of for i

	return resultPtr;
}//Of transposeCompressedMatrix

/**
 * Test the compressed matrix.
 */
void compressedMatrixTest(){
	CompressedMatrixPtr tempPtr1, tempPtr2;
	int i, j, tempElements;

	//Construct the first sample matrix.
	tempElements = 4;
	int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
	for(i = 0; i < tempElements; i ++){
		tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
	}//Of for i

	int tempMatrix2[4][3] = {{0, 0, 2}, {0, 2, 3}, {2, 0, 5}, {2, 1, 6}};
	for(i = 0; i < tempElements; i ++){
		for(j = 0; j < 3; j ++) {
			tempMatrix1[i][j] = tempMatrix2[i][j];
		}//Of for j
	}//Of for i
	
	tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);

	printf("After initialization.\r\n");
	printCompressedMatrix(tempPtr1);

	tempPtr2 = transposeCompressedMatrix(tempPtr1);
	printf("After transpose.\r\n");
	printCompressedMatrix(tempPtr2);
}// Of main

/**
 * The entrance.
 */
int main(){
	compressedMatrixTest();

	return 1;
}// Of main
7、运行结果
After initialization.
(0, 0): 2
(0, 2): 3
(2, 0): 5
(2, 1): 6
tempOffsets[1] = 2
tempOffsets[2] = 3
After transpose.
(0, 0): 2
(0, 2): 5
(1, 2): 6
(2, 0): 3

总结:在实现压缩矩阵的转置中,代码中有多次的for循环的运用,在不同的代码块理解起来有一定的难道,所以强烈建议读者朋友们画一画图,在不能理解的地方多次跟踪。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1295986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存