本文分享了利用二维动态数组指针做矩阵运算的实现代码。
1. 头文件
// juzhen 2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdlib.h" #include "windows.h" #define OK 0 #define NG -1 typedef struct mat { int nRow; /* 行数 */ int nCol; /* 列数 */ int* pData; /* 指向矩??体的指? */ }MAT;
2. 程序代码
#include "stdafx.h" #include "Matrix_cal.h" /* Entity and initial matrix of the application matrix function */ int MATAlloc(MAT *pMat, int nRow, int nCol) { pMat->pData = (int *) malloc (nRow * nCol * sizeof(int) ); if(NULL == pMat->pData) { printf("Memary is error!\n"); return NG; } for(int i=0; i<nRow; ++i) { for(int j=0; j<nCol; ++j) { *(pMat->pData + i*nCol + j)=0; } } pMat->nRow = nRow; pMat->nCol = nCol; return OK; } /* Release the memory space and reset the matrix data function */ void MATFree(MAT* pMat) { free(pMat->pData); pMat->pData = NULL; pMat->nRow = 0; pMat->nCol = 0; } /* Import of matrix function */ int MATAssign (MAT* pMat1, const MAT* pMat2) { MATAlloc(pMat1, pMat2->nRow, pMat2->nCol); for(int i=0; i < pMat1->nRow; ++i) { for(int j=0; j < pMat1->nCol; ++j) { *(pMat1->pData + i * pMat1->nCol + j) = *(pMat2->pData + i * pMat1->nCol + j); } } return OK; } /* Matrix sum function */ int MATAdd(const MAT* pMat1, const MAT* pMat2, MAT* pMat3) { MATAlloc(pMat3, pMat1->nRow, pMat1->nCol); if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol)) { for(int i=0; i<pMat1->nRow; ++i) { for(int j=0; j<pMat1->nCol; ++j) { *(pMat3->pData + i * pMat3->nCol + j) = *(pMat1->pData + i * pMat1->nCol + j) + *(pMat2->pData + i * pMat1->nCol + j); } } return OK; } else { printf("Not add!\n"); return NG; } } /* Matrix subtraction function */ int MATSub(const MAT* pMat1, const MAT* pMat2, MAT* pMat3) { MATAlloc(pMat3, pMat1->nRow, pMat1->nCol); if((pMat1->nRow == pMat2->nRow) && (pMat1->nCol == pMat2->nCol)) { for(int i=0; i<pMat1->nRow; ++i) { for(int j=0; j<pMat1->nCol; ++j) { *(pMat3->pData + i * pMat3->nCol + j) = *(pMat1->pData + i * pMat1->nCol + j) - *(pMat2->pData + i * pMat1->nCol + j); } } return OK; } else { printf("Not Sub!\n"); return NG; } } /* Matrix clear */ void MATClear(MAT* pMat) { for(int i=0; i<pMat->nRow; ++i) { for(int j=0; j<pMat->nCol; ++j) { *(pMat->pData + i * pMat->nCol + j)=0; } } } /* Matrix multiplication C function */ void MATMulC (MAT* pMat, int C) { for(int i=0; i<pMat->nRow; ++i) { for(int j=0; j<pMat->nCol; ++j) { *(pMat->pData + i * pMat->nCol + j) = C * (*(pMat->pData + i * pMat->nCol + j) ); } } } /* Matrix multiplication function */ int MATMul (const MAT* pMat1, const MAT* pMat2, MAT* pMat3) { MATAlloc(pMat3, pMat1->nRow, pMat2->nCol); if(pMat1->nCol == pMat2->nRow) { for(int i=0; i<pMat1->nRow; ++i) { for(int j=0; j<pMat2->nCol; ++j) { for(int k=0; k<pMat1->nCol; ++k) { *(pMat3->pData + i * pMat2->nCol+j) += *(pMat1->pData + i * pMat2->nRow + k) * (*(pMat2->pData + k * pMat2->nCol + j) ); } } } return OK; } else { printf("not Mul\n"); return NG; } } /* Matrix transpose function */ int MATTransport(const MAT* pMat1, MAT* pMat2) { MATAlloc(pMat2, pMat1->nCol, pMat1->nRow); for(int i=0; i<pMat1->nRow; ++i) { for(int j=0; j<pMat1->nCol; ++j) { *(pMat2->pData + j * pMat1->nRow + i) = *(pMat1->pData + i * pMat1->nCol + j); } } return OK; } /* bool Check_digit(char *kk) { int a = strlen(kk); for(int i = 0; i<a; ++i) { if( ( (int) (*(kk + i) ) > 48) && ( (int) (*(kk + i) ) < 57 || (int) (*(kk + i) ) == 32) ) { return 1; } } return 0; } */ /* Matrix initialization */ void MATinit(MAT *pMat) { bool kos=1; int nRow = 0, nCol = 0; printf("Please input the number of rows: "); scanf_s("%d",&nRow); putchar('\n'); printf("Please input the number of columns: "); scanf_s("%d",&nCol); putchar('\n'); printf("Please input %dX%d Matrix:\n",nRow,nCol); kos=MATAlloc(pMat,nRow,nCol); for(int i=0; i<nRow; ++i) { for(int j=0; j<nCol; ++j) { scanf("%d", pMat->pData + i*nCol + j); } } } /*char arr[100][100]={0}; for(int i=0; i<nRow; ++i) { for(int j=0; j<nCol; ++j) { scanf("%c", &arr[i][j]); kos = Check_digit(&arr[i][j]); } } //ks= atoi(arr[0]); while(kos) { printf(" input is error,Please input again!"); for(int i=0; i<nRow; ++i) { for(int j=0; j<nCol; ++j) { scanf("%c", arr[i]); } } kos = Check_digit(arr[0]); //ks= atoi(arr[0]); } for(int i=0; i<nRow; ++i) { for(int j=0; j<nCol; ++j) { *(pMat->pData + i*nCol + j) = atoi(&arr[i][j]); } } } */ /* Output matrix */ void Print(MAT *pMat) { printf("The result is:\n"); for(int i = 0; i < pMat->nRow; ++i) { for(int j=0; j<pMat->nCol; ++j) { printf("%d ",*( pMat->pData + i * pMat->nCol + j) ); } putchar('\n'); } } int _tmain(int argc, _TCHAR* argv[]) { int nRow = 1,nCol = 1,sign = 1,C = 1,work = 1,sigal=0; MAT Mat, Mat1, Mat2; MAT *pMat = &Mat; MAT *pMat1 = &Mat1; MAT *pMat2 = &Mat2; while(work) { system("cls"); printf(" Welcome To The Matrix Operation system! \n"); printf("------------------------------------------------\n"); printf("1: Open The Generating matrix function!\n"); printf("2: Open The Release matrix function!\n"); printf("3: Open The Import matrix function!\n"); printf("4: Open The Add matrix function!\n"); printf("5: Open The Matrix subtraction function!\n"); printf("6: Open The Clear matrix function!\n"); printf("7: Open The Matrix multiplication C function!\n"); printf("8: Open The Matrix multiplication function!\n"); printf("9: Open The Matrix transpose function!\n"); printf("------------------------------------------------\n"); printf("Please Select operation type:"); scanf("%d",&sign); switch(sign) { case 1: { MATinit(pMat); Print(pMat); } break; case 2: { MATinit(pMat); Print(pMat); MATFree(pMat); } break; case 3: { MATinit(pMat2); MATAssign (pMat1, pMat2); Print(pMat1); } break; case 4: { MATinit(pMat1); MATinit(pMat2); sigal = MATAdd(pMat1, pMat2,pMat); if(0 == sigal) { Print(pMat); } } break; case 5: { MATinit(pMat1); MATinit(pMat2); sigal = MATSub(pMat1, pMat2,pMat); if(0 == sigal) { Print(pMat); } } break; case 6: { MATinit(pMat); Print(pMat); MATClear(pMat); Print(pMat); } break; case 7: { printf("Please input the number of C: "); scanf("%d",&C); putchar('\n'); MATinit(pMat); MATMulC (pMat, C); Print(pMat); } break; case 8: { MATinit(pMat1); MATinit(pMat2); sigal = MATMul (pMat1, pMat2, pMat); if(0 == sigal) { Print(pMat); } } break; case 9: { MATinit(pMat1); MATTransport(pMat1, pMat2); Print(pMat2); } break; default: printf("input is error!"); } printf("Whether exit the Matrix calculation system?(1 is not exit,0 is exit)\n"); //whether exit the system. scanf("%d", &work); fflush(stdin); while (work != 0 && work != 1) //work must is 1 or 0. { printf(" Input is error,Please input again!\n"); scanf("%d", &work); fflush(stdin); } } printf("\n-------------Thanks For You Using The Matrix Calculation System !--------------\n"); Sleep(2000); //deley some times. return 0; }
以上就是实现二维动态数组指针做矩阵运算的代码,希望对大家的学习有所帮助。
主要内容:指针数组和二维数组指针的区别二维数组在概念上是二维的,有行和列,但在内存中所有的数组元素都是连续排列的,它们之间没有“缝隙”。以下面的二维数组 a 为例: int a[3][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11} }; 从概念上理解,a 的分布像一个矩阵: 但在内存中,a 的分布是一维线性的,整个数组占用一块连续的内存: C语言中的二维数组是按行排列的,也就是先存放
我需要以正确的二维数组格式打印此内容。哎呀,这是错误的。需要从方法打印。我的输出似乎是一个无限循环。
问题内容: 如何以矩阵框格式打印出简单的int [] [],就像我们在其中手写矩阵的格式那样。简单的循环运行显然无效。如果有帮助,我正在尝试在linux ssh终端中编译此代码。 问题答案: 产生:
本文向大家介绍Python中矩阵创建和矩阵运算方法,包括了Python中矩阵创建和矩阵运算方法的使用技巧和注意事项,需要的朋友参考一下 矩阵创建 1、from numpyimport *; a1=array([1,2,3]) a2=mat(a1) 矩阵与方块列表的区别如下: 2、data2=mat(ones((2,4))) 创建一个2*4的1矩阵,默认是浮点型的数据,如果需要时int类型,可以使用
它建议使用以下机制访问2D数组ARR[5][5]的第I第行的第J第元素: 根据我对指针的粗略了解,我知道数组的名称产生第0行的第0元素的地址,数组的任何整数增量都将产生下一行的基地址。我想在这个节骨眼之前一切都很好。 添加间接运算符是否会导致它产生该行的第I个元素,而不是第I个行的基元素的地址? 下面不应该是访问第I第行的第J第元素的代码吗? 在前面提到的情况下,增加arr i次将导致代码片段指向
我已经从图像中加载了一个数字高程图图像(一个浮点高度图),我正在数组中的每个2x2平方子矩阵上迭代,并执行计算并对结果求和。 此操作非常慢,因为我正在使用的高程图可能非常大(16Kx16K),而纯 Python 循环方法比 numpy 或 scipy 慢得多(或者我是这么读的)。但是,我找不到有关如何迭代多维 numpy 数组块的任何具体信息。 例如,如果我有以下3x3 Numpy数组(请记住,这
7. 指向数组的指针与多维数组 指针可以指向复合类型,上一节讲了指向指针的指针,这一节学习指向数组的指针。以下定义一个指向数组的指针,该数组有10个int元素: int (*a)[10]; 和上一节指针数组的定义int *a[10];相比,仅仅多了一个()括号。如何记住和区分这两种定义呢?我们可以认为[]比*有更高的优先级,如果a先和*结合则表示a是一个指针,如果a先和[]结合则表示a是一个数组。
本文向大家介绍pytorch 转换矩阵的维数位置方法,包括了pytorch 转换矩阵的维数位置方法的使用技巧和注意事项,需要的朋友参考一下 例如: 以上这篇pytorch 转换矩阵的维数位置方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。