源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

利用C++实现矩阵的相加/相称/转置/求鞍点

  • 时间:2020-06-30 11:26 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:利用C++实现矩阵的相加/相称/转置/求鞍点
[b]1.矩阵相加[/b] 两个同型矩阵做加法,就是对应的元素相加。
[u]复制代码[/u] 代码如下:
#include<iostream> using namespace std; int main(){  int a[3][3]={{1,2,3},{6,5,4},{4,3,2}};  int b[3][3]={{4,3,2},{6,5,4},{1,2,3}};  int c[3][3]={0,0,0,0,0,0,0,0,0};  int i,j;  cout<<"Array A:"<<endl;  for(i=0;i<3;i++){   for(j=0;j<3;j++){    c[i][j]+=a[i][j];//实现相加操作1    cout<<"\t"<<a[i][j];//输出矩阵A   }   cout<<endl;  }  cout<<endl;  cout<<"Array B:"<<endl;  for(i=0;i<3;i++){   for(j=0;j<3;j++){    c[i][j]+=b[i][j];//实现矩阵操作2    cout<<"\t"<<b[i][j];//输出矩阵B   }   cout<<endl;  }  cout<<endl;  cout<<"Array C:"<<endl;  for(i=0;i<3;i++){   for(j=0;j<3;j++){    cout<<"\t"<<c[i][j];//输出矩阵C   }   cout<<endl;  }  cout<<endl;  return 0; }
[img]http://files.jb51.net/file_images/article/201310/201310210834492.jpg[/img] [b]2.实现矩阵的转置 [/b]
[u]复制代码[/u] 代码如下:
#include<iostream> using namespace std; int main(){  int a[3][2]={{4,3},{6,5},{1,2}};  int b[2][3]={0,0,0,0,0,0};  int i,j;  cout<<"Array A:"<<endl;     for(i=0;i<3;i++){      for(j=0;j<2;j++){       cout<<"\t"<<a[i][j];//输出矩阵A       b[j][i]=a[i][j];//进行转置操作      }      cout<<endl;     }     cout<<endl;     cout<<"Array B:"<<endl;     for(i=0;i<2;i++){      for(j=0;j<3;j++){       cout<<"\t"<<b[i][j];      }      cout<<endl;     }     cout<<endl;  return 0; }
[img]http://files.jb51.net/file_images/article/201310/201310210834533.jpg[/img] [b]3.实现矩阵的相乘[/b] 一个m行n列的矩阵可以和n列k行的矩阵相乘,得到一个m行k列的矩阵
[u]复制代码[/u] 代码如下:
#include<iostream> using namespace std; int main(){  int a[3][2]={{4,3},{6,5},{1,2}};  int b[2][3]={{1,2,3},{6,5,4}};  int c[3][3]={0,0,0,0,0,0,0,0,0};  int i,j,k,l;  cout<<"Array A:"<<endl;  for(i=0;i<3;i++){   for(j=0;j<2;j++){    cout<<"\t"<<a[i][j];//输出矩阵A   }   cout<<endl;  }  cout<<endl;  cout<<"Array B:"<<endl;  for(i=0;i<2;i++){   for(j=0;j<3;j++){    cout<<"\t"<<b[i][j];//输出矩阵B   }   cout<<endl;  }  cout<<endl;  cout<<"Array C:"<<endl;  for(i=0;i<3;i++){   for(j=0;j<3;j++){      for(k=0;k<2;k++){         c[i][j]+=a[i][k]*b[k][j];//实现相乘操作      }      cout<<"\t"<<c[i][j];//输出矩阵C   }   cout<<endl;  }  cout<<endl;  return 0; }
[img]http://files.jb51.net/file_images/article/201310/201310210834534.jpg[/img] [b]4.求矩阵中的鞍点 [/b] 在矩阵中行中最大,列中最小的元素就是我们要求的鞍点
[u]复制代码[/u] 代码如下:
#include<iostream> using namespace std; int main(){     int a[3][4]={{3,2,13,1},{8,7,10,5},{12,11,14,9}};  int i,j,k,ad,q=0;  bool tag;  for(i=0;i<3;i++){   for(j=0;j<4;j++){    cout<<"\t"<<a[i][j];   }   cout<<endl;  }  cout<<endl;  for(i=0;i<3;i++){   ad=a[i][0];   tag=true;   for(j=1;j<4;j++){    if(ad<a[i][j]){     k=j;    }//先选出行中最大   }   for(j=0;j<3;j++){    if(a[i][k]>a[j][k]){     tag=false;    };//再选出列中最小   }   cout<<endl;   if(tag==true){    cout<<"鞍点是第"<<(i+1)<<"行,第"<<(k+1)<<"列的"<<a[i][k]<<endl;    q++;   }  }  if(q==0){   cout<<"没有一个鞍点~"<<endl;  }  cout<<endl;  return 0; }
[img]http://files.jb51.net/file_images/article/201310/201310210834535.jpg[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部