压缩矩阵
压缩矩阵是将一个矩阵中值为0的各元素删除,只保留下不为0的一串数,每一个数都包含这个数处于原矩阵的行、列和数值三部分。
基本操作
1.定义
//压缩矩阵的元素
typedef struct Triple
{
int i;
int j;
elem e;
} Triple, *TriplePtr;
//压缩矩阵
typedef struct CompressedMatrix
{
int rows,columns,numElems;
Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;
2.打印
//打印
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
for(int i = 0;i< paraPtr->numElems;i++)
{
printf("(%d, %d): %d\n",paraPtr->elements[i].i,paraPtr->elements[i].j,paraPtr->elements[i].e);
}
}
3.初始化
//初始化
CompressedMatrixPtr initCompressedMatrix(int paraRows,int paraColumns,int paraNumElems,int** paraData)
{
int i;
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraRows;
resultPtr->columns = paraColumns;
resultPtr->numElems = paraNumElems;
resultPtr->elements = (TriplePtr)malloc(paraNumElems * sizeof(struct Triple));
for(i = 0;i < paraNumElems;i++)
{
resultPtr->elements[i].i = paraData[i][0];
resultPtr->elements[i].j = paraData[i][1];
resultPtr->elements[i].e = paraData[i][2];
}
return resultPtr;
}
矩阵转置
矩阵转置是将一个m×n的矩阵换成同序数的列得到一个n×m的矩阵。我们进行压缩矩阵的转置,将矩阵以压缩矩阵的形势表现出来。其具体操作不易理解,经过多次具体实例一步步分析后才掌握方法,以下是操作过程:
1.代码
//矩阵倒置
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
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;
}
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraPtr->columns;
resultPtr->columns = paraPtr->rows;
resultPtr->numElems = paraPtr->numElems;
resultPtr->elements = (TriplePtr)malloc(paraPtr->numElems * sizeof(struct Triple));
for(i = 0; i < paraPtr->numElems; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}
tempOffsets[0] = 0;
printf("tempOffsets[0] = 0 \n");
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
}
for(i = 0; i < paraPtr->numElems; 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]++;
}
return resultPtr;
}
2.测试及结果
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//建立矩阵
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}
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];
}
}
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("初始化后.\r\n");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("倒置后.\r\n");
printCompressedMatrix(tempPtr2);
}
初始化后.
(0, 0): 2
(0, 2): 3
(2, 0): 5
(2, 1): 6
tempOffsets[0] = 0
tempOffsets[1] = 2
tempOffsets[2] = 3
倒置后.
(0, 0): 2
(0, 2): 5
(1, 2): 6
(2, 0): 3
总代码
#include <stdio.h>
#include <malloc.h>
typedef int elem;
//压缩矩阵的元素
typedef struct Triple
{
int i;
int j;
elem e;
} Triple, *TriplePtr;
//压缩矩阵
typedef struct CompressedMatrix
{
int rows,columns,numElems;
Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;
//初始化
CompressedMatrixPtr initCompressedMatrix(int paraRows,int paraColumns,int paraNumElems,int** paraData)
{
int i;
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraRows;
resultPtr->columns = paraColumns;
resultPtr->numElems = paraNumElems;
resultPtr->elements = (TriplePtr)malloc(paraNumElems * sizeof(struct Triple));
for(i = 0;i < paraNumElems;i++)
{
resultPtr->elements[i].i = paraData[i][0];
resultPtr->elements[i].j = paraData[i][1];
resultPtr->elements[i].e = paraData[i][2];
}
return resultPtr;
}
//打印
void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
for(int i = 0;i< paraPtr->numElems;i++)
{
printf("(%d, %d): %d\n",paraPtr->elements[i].i,paraPtr->elements[i].j,paraPtr->elements[i].e);
}
}
//矩阵倒置
CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
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;
}
CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
resultPtr->rows = paraPtr->columns;
resultPtr->columns = paraPtr->rows;
resultPtr->numElems = paraPtr->numElems;
resultPtr->elements = (TriplePtr)malloc(paraPtr->numElems * sizeof(struct Triple));
for(i = 0; i < paraPtr->numElems; i ++)
{
tempColumnCounts[paraPtr->elements[i].j] ++;
}
tempOffsets[0] = 0;
printf("tempOffsets[0] = 0 \n");
for(i = 1; i < paraPtr->columns; i ++)
{
tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
}
for(i = 0; i < paraPtr->numElems; 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]++;
}
return resultPtr;
}
//测试
void compressedMatrixTest()
{
CompressedMatrixPtr tempPtr1, tempPtr2;
int i, j, tempElements;
//建立矩阵
tempElements = 4;
int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
for(i = 0; i < tempElements; i++)
{
tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
}
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];
}
}
tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);
printf("初始化后.\r\n");
printCompressedMatrix(tempPtr1);
tempPtr2 = transposeCompressedMatrix(tempPtr1);
printf("倒置后.\r\n");
printCompressedMatrix(tempPtr2);
}
int main()
{
compressedMatrixTest();
return 1;
}