码迷,mamicode.com
首页 > 编程语言 > 详细

C++实现矩阵卷积

时间:2015-06-18 13:06:14      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

int kernel_x;
int kernel_y;
int mat_x;
int mat_y;
int **kernel;
int **mat;
int pos_archor_x;
int pos_archor_y;
void getValue(int pos_x,int pos_y)
{
	int temp_x_kernel_start,temp_x_kernel_end;
	int temp_y_kernel_start,temp_y_kernel_end;
	if(pos_x>=pos_archor_x)
		temp_x_kernel_start=0;
	else
		temp_x_kernel_start=pos_archor_x-pos_x;
	if(mat_x-pos_x>=kernel_x-pos_archor_x)
		temp_x_kernel_end=kernel_x-1;
	else
		temp_x_kernel_end=mat_x-1-pos_x+pos_archor_x;
	if(pos_y>=pos_archor_y)
		temp_y_kernel_start=0;
	else
		temp_y_kernel_start=pos_archor_y-pos_y;
	if(mat_y-pos_y>=kernel_y-pos_archor_y)
		temp_y_kernel_end=kernel_y-1;
	else
		temp_y_kernel_end=mat_y-1-pos_y+pos_archor_y;

	cout<<"start"<<"("<<temp_x_kernel_start<<","<<temp_y_kernel_start<<")"<<‘\t‘<<"end"<<"("<<temp_x_kernel_end<<","<<temp_y_kernel_end<<")"<<endl;
	
	int all=0;
	
	int x=0;
	
	for(int i=temp_x_kernel_start;i<=temp_x_kernel_end;++i)
	{
			int y=0;
		
		for(int j=temp_y_kernel_start;j<=temp_y_kernel_end;++j)
		{
			cout<<"("<<pos_x-(pos_archor_x-temp_x_kernel_start)+x<<‘,‘<<pos_y-(pos_archor_y-temp_y_kernel_start)+y<<")"<<endl;
			all+=kernel[i][j]*mat[pos_x-(pos_archor_x-temp_x_kernel_start)+x][pos_y-(pos_archor_y-temp_y_kernel_start)+y];
			cout<<all<<endl;
			y++;
		}
		x++;
	}
	mat[pos_x][pos_y]=all;

	
}

int main()
{
	cout<<"input the kernel size"<<endl;
	cout<<"input the kernel‘x lenth"<<endl;
	cin>>kernel_x;
	cout<<"input the kernel‘y lenth"<<endl;
	cin>>kernel_y;
	kernel=new int*[kernel_x];
	for(int i=0;i!=kernel_x;++i)
		kernel[i]=new int[kernel_y];
	cout<<"please input the kernel"<<endl;
	for(int i=0;i!=kernel_x;++i)
		for(int j=0;j!=kernel_y;++j)
		cin>>kernel[i][j];
	cout<<"input the mat size"<<endl;
	cout<<"input the mat‘x lenth"<<endl;
	cin>>mat_x;
	cout<<"input the mat‘y lenth"<<endl;
	cin>>mat_y;
	mat=new int*[mat_x];
	for(int i=0;i!=mat_x;++i)
		mat[i]=new int[mat_y];
	cout<<"please inpute the mat"<<endl;
	for(int i=0;i!=mat_x;++i)
	{
		for(int j=0;j!=mat_y;++j)
			cin>>mat[i][j];

	}
	pos_archor_x=0;
	pos_archor_y=1;
	for(int i=0;i!=mat_x;++i)
		for(int j=0;j!=mat_y;++j)
          getValue(i,j);
	

}

  

C++实现矩阵卷积

标签:

原文地址:http://www.cnblogs.com/dmq5488287/p/4585448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!