• 数组与链表算法-矩阵算法


    目录

     数组与链表算法-矩阵算法

    矩阵相加

    C++代码

    矩阵相乘

    C++代码

    转置矩阵

    C++代码

    稀疏矩阵

    C++代码


     数组与链表算法-矩阵算法

    矩阵相加

    矩阵的相加运算较为简单,前提是相加的两个矩阵对应的行数与列数必须相等,而相加后矩阵的行数与列数也是相同的。【

    C++代码

    1. #include
    2. using namespace std;
    3. void MaterixAdd(int* arrA, int* arrB, int* arrC, int dimX, int dimY) {
    4. if (dimX <= 0 || dimY <= 0) {
    5. cout << "矩阵维数必须大于0" << endl;
    6. return;
    7. }
    8. for (int row = 0; row < dimX; row++) {
    9. for (int col = 0; col < dimY; col++) {
    10. arrC[row * dimX + col] = arrA[row * dimX + col] + arrB[row * dimX + col];
    11. }
    12. }
    13. }
    14. void PrintArr(int* arr, int dimX, int dimY) {
    15. for (int i = 0; i < dimX; i++) {
    16. for (int j = 0; j < dimY; j++) {
    17. cout << arr[i * dimX + j] << "\t";
    18. }
    19. cout << endl;
    20. }
    21. }
    22. int main() {
    23. const int Row = 3;
    24. const int Col = 3;
    25. int A[Row][Col] = { {1,3,5},
    26. {7,9,11},
    27. {13,15,17} };
    28. int B[Row][Col] = { {9,8,7},
    29. {6,5,4},
    30. {3,2,1} };
    31. int C[Row][Col] = { 0 };
    32. cout << "矩阵A的各个元素:" << endl;
    33. PrintArr(&A[0][0], Row, Col);
    34. cout << "矩阵B的各个元素:" << endl;
    35. PrintArr(&B[0][0], Row, Col);
    36. MaterixAdd(&A[0][0], &B[0][0], &C[0][0], Row, Col);
    37. cout << "矩阵C的各个元素:" << endl;
    38. PrintArr(&C[0][0], Row, Col);
    39. return 0;
    40. }

    输出结果

     

     

    矩阵相乘

    两个矩阵A和B的相乘受到某些条件的限制。首先,必须符合A为一个m\times n的矩阵,B为一个n\times p的矩阵,A\times B之后的结果为一个m\times p的矩阵C。

    C++代码

    1. #include
    2. using namespace std;
    3. void MaterixMultiply(int* arrA, int* arrB, int* arrC, int dimX, int dimY) {
    4. if (dimX <= 0 || dimY <= 0) {
    5. cout << "矩阵维数必须大于0" << endl;
    6. return;
    7. }
    8. for (int i = 0; i < dimX; i++) {
    9. for (int j = 0; j < dimX; j++) {
    10. int Temp = 0;
    11. for(int k = 0;k< dimY;k++){
    12. arrC[i * dimX + j] += (arrA[i * dimY + k] * arrB[k * dimX + j]);
    13. }
    14. }
    15. }
    16. }
    17. void PrintArr(int* arr, int dimX, int dimY) {
    18. for (int i = 0; i < dimX; i++) {
    19. for (int j = 0; j < dimY; j++) {
    20. cout << arr[i * dimY + j] << "\t";
    21. }
    22. cout << endl;
    23. }
    24. }
    25. int main() {
    26. const int Row = 2;
    27. const int Col = 3;
    28. int A[Row][Col] = { {1,2,3},
    29. {4,5,6} };
    30. int B[Col][Row] = { {3,4},
    31. {6,1},
    32. {2,7} };
    33. int C[Row][Row] = { 0 };
    34. cout << "矩阵A的各个元素:" << endl;
    35. PrintArr(&A[0][0], Row, Col);
    36. cout << "矩阵B的各个元素:" << endl;
    37. PrintArr(&B[0][0], Col, Row);
    38. MaterixMultiply(&A[0][0], &B[0][0], &C[0][0], Row, Col);
    39. cout << "矩阵C的各个元素:" << endl;
    40. PrintArr(&C[0][0], Row, Row);
    41. return 0;
    42. }

    输出结果

    转置矩阵

    转置矩阵(A^{t})就是把原矩阵的行坐标元素与列坐标元素相互调换。假设A^{t}A的转置矩阵,则有A^{t}[j,i] = A[i,j]

    C++代码

    1. #include
    2. using namespace std;
    3. void MaterixTranspose(int* arr, int dimX, int dimY) {
    4. for (int i = 0; i < dimX; i++) {
    5. for (int j = 0; j <= i; j++) {
    6. int Temp = arr[i * dimY + j];
    7. arr[i * dimY + j] = arr[j * dimY + i];
    8. arr[j * dimY + i] = Temp;
    9. }
    10. }
    11. }
    12. void PrintArr(int* arr, int dimX, int dimY) {
    13. for (int i = 0; i < dimX; i++) {
    14. for (int j = 0; j < dimY; j++)
    15. cout << arr[i * dimY + j] << "\t";
    16. cout << endl;
    17. }
    18. }
    19. int main() {
    20. const int Row = 3;
    21. const int Col = 3;
    22. int arr[Row][Col] = { {1,2,3},
    23. {4,5,6},
    24. {7,8,9} };
    25. cout << "原始矩阵:" << endl;
    26. PrintArr(&arr[0][0], Row, Col);
    27. MaterixTranspose(&arr[0][0], Row, Col);
    28. cout << "转置矩阵:" << endl;
    29. PrintArr(&arr[0][0], Row, Col);
    30. return 0;
    31. }

    输出结果

    稀疏矩阵

    稀疏矩阵(Sparse Matrix)就是指一个矩阵中的大部分元素为0。对于稀疏矩阵而言,因为矩阵中的许多元素都是0,所以实际存储的数据项很少,如果在计算机中使用传统的二维数组方式来存储稀疏矩阵,就十分浪费计算机的内存空间。

    提高内存空间利用率的方法是使用三项式(3-Tuple)的数据结构。我们把每一个非零项用(i, j, item-value)的形式来表示,假如一个稀疏矩阵有n个非零项,那么可以使用一个A(0:n, 1:3)的二维数组来存储这些非零项。

    其中,A(0, 1)存储这个稀疏矩阵的行数,A(0,2)存储这个稀疏矩阵的列数,而A(0,3)则存储这个稀疏矩阵非零项的总数。另外,每一个非零项以(i, j, item-value)来表示。其中i为此矩阵非零项所在的行数,j为此矩阵非零项所在的列数,item-value则为此矩阵非零项的值。

    A(0, 1):表示此矩阵的行数。

    A(0, 2):表示此矩阵的列数。

    A(0, 3):表示此矩阵非零项的总数。

    这种利用3项式数据结构来压缩稀疏矩阵的方式可以减少对内存的浪费。

    C++代码

    1. #include
    2. using namespace std;
    3. void MaterixSparse(int* arrSparse, int* arrCompress, int dimX, int dimY) {
    4. int temp = 1;
    5. for (int i = 0; i < dimX; i++) {
    6. for (int j = 0; j < dimY; j++) {
    7. if (arrSparse[i * dimY + j] != 0) {
    8. arrCompress[temp * 3 + 0] = i;
    9. arrCompress[temp * 3 + 1] = j;
    10. arrCompress[temp * 3 + 2] = arrSparse[i * dimY + j];
    11. temp++;
    12. }
    13. }
    14. }
    15. }
    16. void PrintArr(int* arr, int dimX, int dimY) {
    17. for (int i = 0; i < dimX; i++) {
    18. for (int j = 0; j < dimY; j++)
    19. cout << arr[i * dimY + j] << "\t";
    20. cout << endl;
    21. }
    22. }
    23. int main() {
    24. const int Row = 8;
    25. const int Col = 9;
    26. const int NotZero = 8;
    27. int Sparse[Row][Col] = { {0,0,0,0,0,0,0,0,0},
    28. {0,0,0,0,0,0,0,0,0},
    29. {0,0,0,0,7,0,0,0,0},
    30. {0,0,0,0,0,0,0,0,5},
    31. {0,0,0,0,0,0,0,0,0},
    32. {0,0,0,0,0,0,0,0,0},
    33. {0,0,6,1,8,0,0,0,2},
    34. {4,0,0,0,0,0,3,0,0} };
    35. int Compress[NotZero + 1][3]{ 0 };
    36. Compress[0][0] = Row;
    37. Compress[0][1] = Col;
    38. Compress[0][2] = NotZero;
    39. cout << "稀疏矩阵:" << endl;
    40. PrintArr(&Sparse[0][0], Row, Col);
    41. MaterixSparse(&Sparse[0][0], &Compress[0][0], Row, Col);
    42. cout << "压缩矩阵:" << endl;
    43. PrintArr(&Compress[0][0], NotZero + 1, 3);
    44. return 0;
    45. }

    输出结果

  • 相关阅读:
    前端面试题50(有哪些常见的前端安全措施可以防止CSRF攻击?)
    【GitHub】小技巧
    基于JAVA清颜广告股份有限公司网站演示录像计算机毕业设计源码+数据库+lw文档+系统+部署
    顶级网络安全预测:近三分之一的国家将在三年内规范勒索软件响应
    【css案例】
    ubuntu 18.04安装自己ko驱动&& 修改secure boot
    HDD-FAT32 ZIP-FAT32 HDD-FAT16 ZIP-FAT16 HDD-NTFS
    Flink之窗口指派API模板
    k8s~动态生成pvc和pv
    阶乘算法优化
  • 原文地址:https://blog.csdn.net/qq_40660998/article/details/134059399