| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1323 人关注过本帖
标题:[原创]两矩阵相乘,希望对大家有用
只看楼主 加入收藏
hlstone
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-12-13
收藏
 问题点数:0 回复次数:3 
[原创]两矩阵相乘,希望对大家有用


#include <iostream.h>
#include <iomanip.h>

int a[3][4]={{ 5, 7, 8, 2},
{-2, 4, 1, 1},
{ 1, 2, 3, 4}};

int b[4][5]={{4,-2, 3, 3, 9},
{4, 3, 8,-1, 2},
{2, 3, 5, 2, 7},
{1, 0, 6, 3, 4}};

int c[3][5];

bool MultiMatrix(int a[3][4], int arow, int acol,
int b[4][5], int brow, int bcol,
int c[3][5], int crow, int ccol); //函数声明

void main()
{
if(MultiMatrix(a,3,4, b,4,5, c,3,5)){
cout <<"illegal matrix multiply.\n";
return;
}

for(int i=0; i<3; i++){ //输出矩阵乘法的结果
for(int j=0; j<5; j++)
cout <<setw(5) <<c[i][j];
cout <<endl;
}
}

bool MultiMatrix(int a[3][4], int arow, int acol,
int b[4][5], int brow, int bcol,
int c[3][5], int crow, int ccol)
{
if(!((acol==brow)&&(crow==arow)&&(ccol==bcol))) //正确性检查
return 1;

for(int i=0; i<crow; i++) //行
for(int j=0; j<ccol; j++){ //列
c[i][j]=0; //此句可以省略,因为c是全局数组
for(int n=0; n<acol; n++) //求一个元素
c[i][j]+= a[i][n] * b[n][j];
}

return 0;
}

搜索更多相关主题的帖子: int 矩阵 相乘 include bool 
2005-12-13 23:50
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
有什么用呢????

=×&D o I p R e E n C g T l X&×=
2005-12-14 12:21
pudding
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-12-14
收藏
得分:0 

可不可以帮我看一看这哪里错了,矩阵相乘为什么最后输出的是乱七八糟的东西呢
#include<iostream.h>
#include<stdio.h>
#define N 2
void main()
{int a[N][N],b[N][N];
//input 1
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
cin>>a[i][j];
//input 2
int m,n;
for(m=0;m<N;m++)
for(n=0;n<N;n++)
cin>>b[m][n];
 //矩阵相加

int c[N][N];
for(i=0;i<N;i++)
for(j=0;j<N;j++)
c[i][j]=a[i][j]+b[i][j];
//display c
for(m=0;m<N;m++)
{ for(n=0;n<N;n++)
printf("%d ",c[m][n]);
if(j%(N-1)==0 )
cout<<endl;}
// 矩阵相乘
 
 
  int e[N][N];
for(i=0;i<N;i++)
{
for(m=0;m<N;m++)
for(n=0;n<N;n++)
e[i][m]+=a[i][n]*b[n][m];
}

for(i=0;i<N;i++)
{for(j=0;j<N;j++)
cout<<e[i][j]<<" ";
if(j%(N-1)==0 )
cout<<endl;}

}

2005-12-14 22:26
flame
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2005-12-15
收藏
得分:0 
这是我写的矩阵的基本运算,希望对你有帮助,有什么不足也谢谢指出.
#include<iostream.h>
#include<math.h>
class A
{
public:
A()
{
int i,j,k;
cout<<"请输入矩阵的行数和列数: ";
cin>>go>>row;
elem=new double *[go];
for(i=0;i<go;i++)
elem[i]=new double[row];
cout<<"请输入矩阵的元素."<<endl;
for(j=0;j<go;j++)
{
cout<<"请输入第"<<j+1<<"行的"<<row<<"个数:";
for(k=0;k<row;k++)
cin>>elem[j][k];
}
}
A(int i,int j)
{
go=i;row=j;
elem=new double *[i];
for(int k=0;k<go;k++)
elem[k]=new double[j];
}
A(A &a1)
{
go=a1.go;
row=a1.row;
elem=new double *[go];
for(int i=0;i<go;i++)
elem[i]=new double[row];
int k,l;
for(k=0;k<go;k++)
for(l=0;l<row;l++)
elem[k][l]=a1.elem[k][l];
}
~A()
{
for(int i=0;i<go;i++)
{
delete elem[i];
}
delete elem;
}
void print()
{
int i,j;
for(i=0;i<go;i++)
{
for(j=0;j<row;j++)
cout<<elem[i][j]<<' ';
cout<<endl;
}
}
void print1(const A &a1,const A &a2)
{
int i,j;
for(i=0;i<a1.go;i++)
{
for(j=0;j<a2.row;j++)
cout<<elem[i][j]<<' ';
cout<<endl;
}
}
int getgo()
{
return go;
}
int getrow()
{
return row;
}
friend A operator +(A &a1,A &a2);
friend A operator -(A &a1,A &a2);
friend A operator *(A &a1,A &a2);
A & A::operator =(A &a1);
private:
int go,row;
double **elem;
};
A & A::operator =(A &a1)
{
if(this==&a1)
return *this;
else
{
for(int i=0;i<go;i++)
{
delete elem[i];
}
delete elem;
go=a1.go;
row=a1.row;
elem=new double *[go];
for(i=0;i<go;i++)
elem[i]=new double[row];
int t,u;
for(t=0;t<a1.go;t++)
{
for(u=0;u<a1.row;u++)
elem[t][u]=a1.elem[t][u];
}
return *this;
}
}
A operator +(A &a1,A &a2)
{
A a3(a1.getgo(),a1.getrow());
int t,u;
for(t=0;t<a1.go;t++)
{
for(u=0;u<a1.row;u++)
a3.elem[t][u]=a1.elem[t][u]+a2.elem[t][u];
}
return a3;
}
A operator -(A &a1,A &a2)
{
A a3(a1.getgo(),a1.getrow());
int t,u;
for(t=0;t<a1.go;t++)
{
for(u=0;u<a1.row;u++)
a3.elem[t][u]=a1.elem[t][u]-a2.elem[t][u];
}
return a3;
}
A operator *(A &a1,A &a2)
{
A a3(a1.getgo(),a2.getrow());
int t,u,c;
for(t=0;t<a1.go;t++)
{
for(u=0;u<a2.row;u++)
{
a3.elem[t][u]=0;
for(c=0;c<a1.row;c++)
a3.elem[t][u]+=a1.elem[t][c]*a2.elem[c][u];
}
}
return a3;
}
void main()
{
A a1;
a1.print();
A a2;
a2.print();
A a3(0,0);
if((a1.getgo()==a2.getgo())&&(a1.getrow()==a2.getrow()))
{
cout<<"两个矩阵的和为:"<<endl;
a3=a1+a2;
a3.print();
cout<<"两个矩阵的差为:"<<endl;
a3=a1-a2;
a3.print();
}
else
cout<<"因为两矩阵的行列不等,所以两个矩阵不能加减。"<<endl;
if(a1.getrow()==a2.getgo())
{
cout<<"两个矩阵的积为:"<<endl;
a3=a1*a2;
a3.print();
}
else
cout<<"因为第一个矩阵的列不等于第二个矩阵的行,所以两矩阵无法相乘。"<<endl;
}

随风飘逝,叶子与我飞翔,随海飘逝,鱼儿与我追逐,随心飘逝,却只有寂寞与我相伴!
2005-12-15 17:58
快速回复:[原创]两矩阵相乘,希望对大家有用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015726 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved