C++中关于二维矩阵和三维矩阵的计算

1、二维数据矩阵的乘法:

#include<iostream>
#include<math.h>
#include<string>
using namespace std;
int main()
{
	int m1, n1, m2, n2;
	cout << "请分别输入第一个矩阵的行数m1和列数n1:";
	cin >> m1 >> n1;
	cout << "请分别输入第二个矩阵的行数m2和列数n2:";
	cin >> m2 >> n2;
	float** M1 = new float* [m1];
	float** M2 = new float* [m2];
	float** M3 = new float* [m1];
	for (int i = 0; i < m1; i++)
	{
		M1[i] = new float[n1];
	}
	for (int i = 0; i < m2; i++)
	{
		M2[i] = new float[n2];
	}
	for (int i = 0; i < m1; i++)
	{
		M3[i] = new float[n2];
	}
	for (int i = 0; i < m1; i++)
	{
		for (int j = 0; j < n1; j++)
		{
			M1[i][j] = i + j;
		}
	}
	for (int i = 0; i < m2; i++)
	{
		for (int j = 0; j < n2; j++)
		{
			M2[i][j] = i + j + 1;
		}
	}
	for (int i = 0; i < m1; i++)
	{
		for (int j = 0; j < n1; j++)
		{
			cout << M1[i][j] << "\t";
		}
		cout << endl;
	}
	cout << endl << endl;
	for (int i = 0; i < m2; i++)
	{
		for (int j = 0; j < n2; j++)
		{
			cout << M2[i][j] << "\t";
		}
		cout << endl;
	}
	cout << endl << endl;
	//二维动态数组的定义与赋值

	if (n1 == m2)//做一个简单的判断  是否满足矩阵相乘的规则
	{
		for (int i = 0; i < m1; i++)
		{
			for (int j = 0; j < n2; j++)
			{
				M3[i][j] = 0;
				for (int k = 0; k < n1; k++)
				{
					M3[i][j] = M3[i][j] + M1[i][k] * M2[k][j];
				}
				continue;//该程序的关键之处
			}
		}
		for (int i = 0; i < m1; i++)
		{
			for (int j = 0; j < n2; j++)
			{
				cout << M3[i][j] << "\t";
			}
			cout << endl;
		}
		cout << endl << endl;
	}
	else
		cout << "不满足计算条件!!!" << endl;
	cout << endl << endl;

	delete[]M1;
	delete[]M2;
	delete[]M3;
	cout << endl << endl;
	以上是二维数组的乘法,两个数组分别是M1*N1维和M2*N2维(其中N1=M2)
    system("pause");
	return 0;
}

2、三维矩阵的乘法:

备注:这里主要是参照MATLAB的算法,按照页来计算,本文是按照平行于XY平面为页来进行计算的,故此这里对三维数组的第三维要求必须一样,当然换一个页对应的也有如是的要求。

#include<iostream>
#include<math.h>
#include<string>
using namespace std;
int main()
{
   //接下来练习一下三维数组的乘法  这里按照第三页的乘法(通俗一点就是按照垂直于Z轴切片,然后按“片”进行乘法)
	//动态三维数组的定义
	int l1, w1, h1, l2, w2, h2;
	cout << "请输入第一个三维矩阵的各个维度l1、w1、h1:";
	cin >> l1 >> w1 >> h1;
	cout << "请输入第二个三维矩阵的各个维度l2、w2、h2:";
	cin >> l2 >> w2 >> h2;
	float*** P1 = new float** [l1];
	float*** P2 = new float** [l2];
	float*** P3 = new float** [l1];
	for (int i = 0; i < l1; i++)
	{
		P1[i] = new float* [w1];
		for (int j = 0; j < w1; j++)
		{
			P1[i][j] = new float[h1];
		}
	}
	for (int i = 0; i < l2; i++)
	{
		P2[i] = new float* [w2];
		for (int j = 0; j < w2; j++)
		{
			P2[i][j] = new float[h2];
		}
	}for (int i = 0; i < l2; i++)
	{
		P3[i] = new float* [w2];
		for (int j = 0; j < w2; j++)
		{
			P3[i][j] = new float[h2];
		}
	}
	//三维数组赋初值
	for (int i = 0; i < h1; i++)
	{
		for (int j = 0; j < l1; j++)
		{
			for (int k = 0; k < w1; k++)
			{
				P1[j][k][i] = i + j + k;
			}
		}
	}
	for (int i = 0; i < h1; i++)
	{
		for (int j = 0; j < l1; j++)
		{
			for (int k = 0; k < w1; k++)
			{
				cout << P1[j][k][i] << "\t";
			}
			cout << endl;
		}
		cout << endl << endl;
	}
	cout << endl << endl;
	for (int i = 0; i < h2; i++)
	{
		for (int j = 0; j < l2; j++)
		{
			for (int k = 0; k < w2; k++)
			{
				P2[j][k][i] = i + j + k;
			}
		}
	}
	for (int i = 0; i < h2; i++)
	{
		for (int j = 0; j < l2; j++)
		{
			for (int k = 0; k < w2; k++)
			{
				cout << P2[j][k][i] << "\t";
			}
			cout << endl;
		}
		cout << endl << endl;
	}
	cout << endl << endl;

	//数组乘法运算
	if (h1 == h2 && w1 == l2)
	{
		for (int i = 0; i < h1;i++)
		{
			for (int j = 0; j < l1; j++)
			{
				for (int k = 0; k < w2; k++)
				{
					P3[j][k][i] = 0.0;
					for (int t = 0; t < w1; t++)
					{
						P3[j][k][i] = P3[j][k][i] + P1[j][t][i] * P2[t][k][i];
					}
					continue;//该程序的关键之处
				}
			}
		}
		for (int i = 0; i < h2; i++)
		{
			for (int j = 0; j < l1; j++)
			{
				for (int k = 0; k < w2; k++)
				{
					cout << P3[j][k][i] << "\t";
				}
				cout << endl;
			}
			cout << endl << endl;
		}
	}
	else
	{
		cout << "这两个三维矩阵不能进行乘法运算!!!" << endl;
	}
delete[]P1;
	delete[]P2;
	delete[]P3;
	system("pause");
	return 0;
}

本文在数组的定义上用的是动态数组的定义方法,且用new来进行定义的,也可以用malloc来进行相关定义,语法是差不多的,用该语句在头文件里面记得加上#include<stdio.h>和#include<stdlib.h>,还可以用结构体来定义三维数组。

重点:假如有错或者各位看官有更好的点子欢迎评论一起探讨。

    原文作者:廿三___阿军
    原文地址: https://blog.csdn.net/HCJ19981112/article/details/123318416
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞