C++ 实现矩阵相乘
2018-07-20 来源:open-open
#include <iostream> using namespace std; void MatrixMultiplication(double *m1,double *m2,double *m3,int m,int n,int k) { for(int i=0;i<m;i++) { for(int j=0;j<k;j++) { double sum = 0.0; for(int jj=0;jj<n;jj++) { sum += *(m1+i*n+jj)*(*(m2+jj*k+j)); } *(m3+i*k+j) = sum; } } } int main(int argc, char* argv[]) { int m = 1 , n = 3 , k = 3; double *m1,*m2,*m3; m1 = new double[m*n]; m2 = new double[n*k]; m3 = new double[m*k]; //[50 50 1] *(m1+n*0+0) = 50; *(m1+n*0+1) = 50; *(m1+n*0+2) = 1; //[1 0 0] //[0 1 0] //[200, 200,1] *(m2+k*0+0) = 1; *(m2+k*0+1) = 0; *(m2+k*0+2) = 0; *(m2+k*1+0) = 0; *(m2+k*1+1) = 1; *(m2+k*1+2) = 0; *(m2+k*2+0) = 200; *(m2+k*2+1) = 200; *(m2+k*2+2) = 1; MatrixMultiplication(m1,m2,m3,m,n,k); cout<<"["<<*(m3+k*0+0)<<" "; cout<<*(m3+k*0+1)<<" "; cout<<*(m3+k*0+2)<<"]"<<endl; delete[] m1; delete[] m2; delete[] m3; return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:Java上传下载功能的实现
最新资讯
热门推荐