矩阵的基本运算
1、矩阵创建
2、矩阵相加
3、矩阵相减
4、矩阵相乘
5、数字乘矩阵
6、矩阵上叠加
7、矩阵左右叠加
8、矩阵转置
9、矩阵旋转
10、矩阵求逆
11、矩阵输出
主要运用的知识点:掌握二维指针的动态分配内存
下面举一个简单的例子来理解二维指针的动态分配内存
#include<iostream>
using namespace std;
int main()
{
int a, b; //a是行,b是列
cout << "请输入二维数组的行数和列数:";
cin >> a >> b;
//动态分配内存
int** matrix = new int* [a];
for (int i = 0; i < a; i++)
{
matrix[i] = new int[b];
}
//给二维数组赋值
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
matrix[i][j] = i * b + j;
}
}
//打印二维数组的值
cout << "二维数组的值为;" << endl;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
//释放内存
for (int i = 0; i < a; i++)
{
delete[]matrix[i];
}
delete[]matrix;
system("pause");
return 0;
}
下面是运行结果
解析:
在这个例子中,我们首先从用户那里获取了二维数组的行数和列数。然后,我们使用 new 运算符动态分配了一个指针数组 matrix,其中每个元素都是指向一维整型数组的指针。
接下来,我们使用嵌套循环给二维数组赋值。每个元素的值等于其在数组中的行索引乘以列数加上列索引。
最后,我们打印出整个二维数组的值,并通过 delete 运算符释放了分配的内存。先释放每一行的内存,再释放指针数组本身的内存。
注意:
Matrix类型是type,先为它分配row行内存,内存类型与Matrix一样
Matrix=(type) malloc(rowsizeof(type))
每一行分配col列内存,内存类型与Matix[i]一样,也就是type*
Matrix[i] = (type*)malloc(col * sizeof(type ));
————————————————
版权声明:本文为CSDN博主「Vecace」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011463794/article/details/85223844
代码如下:
type **Matrix ;//row为行,col为列
Matrix=(type **) malloc(row*sizeof(type *)) ;
for(int i=0; i<row; i++)
Matrix[i] = (type *)malloc(col * sizeof(type ));
在这道题上,我还分析了如下内容:
createMatrix(int rows, int cols): 创建一个指定行列数的矩阵,并返回指向该矩阵的指针。
releaseMatrix(int** matrix, int rows): 释放一个矩阵所占用的内存空间。
addMatrix(int** matrix1, int** matrix2, int rows, int cols): 对两个相同维度的矩阵进行相加操作。
subtractMatrix(int** matrix1, int** matrix2, int rows, int cols): 对两个相同维度的矩阵进行相减操作。
multiplyMatrix(int** matrix1, int rows1, int cols1, int** matrix2, int cols2): 对两个矩阵进行相乘操作。
multiplyByScalar(int** matrix, int rows, int cols, int scalar): 将一个矩阵中的每个元素乘以一个标量值。
stackMatrixVertical(int** matrix1, int rows1, int cols, int** matrix2, int rows2): 将两个具有相同列数而行不同的矩阵进行垂直叠加。
stackMatrixHorizontal(int** matrix1, int rows, int cols1, int** matrix2, int cols2): 将两个具有相同行数而列不同的矩阵进行水平叠加。
transposeMatrix(int** matrix, int rows, int cols): 对一个矩阵进行转置操作,行变为列,列变为行。
printMatrix(int** matrix, int rows, int cols): 输出一个矩阵的内容。
rotateMatrix(int** matrix, int rows, int cols): 将一个矩阵逆时针旋转90度。
inverseMatrix(int** matrix, int rows, int cols): 求一个矩阵的逆矩阵。
1.矩阵的创建
矩阵的创建如最上面举的二维指针的动态分配内存一致。
2.矩阵的相加
先创建了两个矩阵 matrix1 和 matrix2,并通过循环输入它们的值。然后,调用 addMatrix 函数对两个矩阵进行相加操作,并将结果保存在 result 中。最后,输出结果并释放动态分配的内存。
3.矩阵的相减
创建两个行列数相同的矩阵matrix3和matrix4,然后用户通过给这两个矩阵赋值,然后调用subtractMatrix函数将matrix3和matrix4相减,得到矩阵result2,并打印输出,最后释放内存。
4.矩阵的相乘
给定两个矩阵matrix5和matrix6,矩阵matrix5的行数为rows1,列数为cols1,矩阵matrix6的列数为cols2。用户通过赋值输入矩阵matrix5和matrix6的元素值,然后调用 multiplyMatrix函数将matrix5和matrix6相乘,得到矩阵result3,并打印输出结果,最后释放内存。
5.数字乘矩阵
用户可以输入一个值,然后调用multiplyByScalar函数将这个值乘以于矩阵的每个元素。然后得到result4,打印出结果矩阵,并释放相关的内存。
6.矩阵上叠加
将两个矩阵在垂直方向上叠加的功能。首先,通过用户输入确定了两个矩阵的行数和列数,并使用createMatrix函数创建了两个矩阵matrix8和matrix9。然后,用户输入了两个矩阵的元素值。接下来,调用stackMatrixVertical函数将两个矩阵进行垂直叠加,并将结果保存到result5矩阵中。最后,使用printMatrix函数打印出叠加后的结果矩阵。最后,通过releaseMatrix函数释放了所动态分配的内存
7.矩阵左右叠加
在这段代码中,用户可以输入两个矩阵的值,并将它们水平堆叠在一起。首先,用户会被要求输入第二个矩阵的列数。然后,程序会提示用户依次输入第一个矩阵和第二个矩阵的元素值。最后,程序会将两个矩阵水平堆叠在一起,并打印出结果矩阵。
createMatrix函数根据指定的行数和列数创建了两个矩阵matrix10和matrix11。
printMatrix函数用于打印给定矩阵的元素。
stackMatrixHorizontal函数接受两个矩阵和它们的行列数作为参数,返回一个水平堆叠后的结果矩阵。释放之前创建的矩阵对象,这通过调用releaseMatrix函数来完成。
8.矩阵的转置
首先创建一个函数createMatrix(rows, cols),用于动态分配二维数组的内存空间,并返回指向该二维数组的指针matrix12。
接着,使用嵌套循环从用户输入读取矩阵的值,将其存储在matrix12数组中。
调用transposeMatrix(matrix12, rows, cols)函数,传入矩阵和行列数,对矩阵进行转置操作,并返回转置后的矩阵的指针result7。
输出转置后的结果,即调用printMatrix(result7, cols, rows)函数,遍历并打印result7数组的元素。
最后,调用releaseMatrix(matrix12, rows)和releaseMatrix(result7, cols)函数,释放matrix12和result7数组所占用的内存空间。
9.矩阵的旋转
参考上上道题的矩阵的旋转
10.矩阵的求逆
在inverseMatrix函数中调用这个新函数,将输入的矩阵作为参数传递,并接收返回的逆矩阵。请确保在计算逆矩阵之前,验证该矩阵是否可逆。如果矩阵不可逆,应打印相应的错误信息并返回合适的值。最后,打印逆矩阵。你可以使用printMatrix函数来实现它。
具体代码如下
#include <iostream>
using namespace std;
// 创建矩阵
int** createMatrix(int rows, int cols)
{
int** matrix = new int* [rows];
for (int i = 0; i < rows; ++i)
{
matrix[i] = new int[cols];
}
return matrix;
}
// 释放矩阵内存
void releaseMatrix(int** matrix, int rows)
{
for (int i = 0; i < rows; ++i)
{
delete[] matrix[i];
}
delete[] matrix;
}
// 矩阵相加
int** addMatrix(int** matrix1, int** matrix2, int rows, int cols)
{
int** result = createMatrix(rows, cols);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return result;
}
// 矩阵相减
int** subtractMatrix(int** matrix1, int** matrix2, int rows, int cols)
{
int** result = createMatrix(rows, cols);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return result;
}
// 矩阵相乘
int** multiplyMatrix(int** matrix1, int rows1, int cols1, int** matrix2, int cols2)
{
int** result = createMatrix(rows1, cols2);
for (int i = 0; i < rows1; ++i)
{
for (int j = 0; j < cols2; ++j)
{
result[i][j] = 0;
for (int k = 0; k < cols1; ++k)
{
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
// 数字乘矩阵
int** multiplyByScalar(int** matrix, int rows, int cols, int scalar)
{
int** result = createMatrix(rows, cols);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
result[i][j] = matrix[i][j] * scalar;
}
}
return result;
}
// 矩阵上叠加
int** stackMatrixVertical(int** matrix1, int rows1, int cols, int** matrix2, int rows2)
{
int** result = createMatrix(rows1 + rows2, cols);
for (int i = 0; i < rows1; ++i)
{
for (int j = 0; j < cols; ++j)
{
result[i][j] = matrix1[i][j];
}
}
for (int i = rows1; i < rows1 + rows2; ++i)
{
for (int j = 0; j < cols; ++j)
{
result[i][j] = matrix2[i - rows1][j];
}
}
return result;
}
// 矩阵左右叠加
int** stackMatrixHorizontal(int** matrix1, int rows, int cols1, int** matrix2, int cols2)
{
int** result = createMatrix(rows, cols1 + cols2);
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols1; ++j)
{
result[i][j] = matrix1[i][j];
}
}
for (int i = 0; i < rows; ++i)
{
for (int j = cols1; j < cols1 + cols2; ++j)
{
result[i][j] = matrix2[i][j - cols1];
}
}
return result;
}
// 矩阵转置
int** transposeMatrix(int** matrix, int rows, int cols)
{
int** result = createMatrix(cols, rows);
for (int i = 0; i < cols; ++i)
{
for (int j = 0; j < rows; ++j)
{
result[i][j] = matrix[j][i];
}
}
return result;
}
// 输出矩阵
void printMatrix(int** matrix, int rows, int cols)
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}
// 矩阵旋转(逆时针90度)
int** rotateMatrix(int** matrix, int rows, int cols)
{
int** result = createMatrix(cols, rows);
for (int i = 0; i < cols; ++i)
{
for (int j = 0; j < rows; ++j)
{
result[i][j] = matrix[j][cols - i - 1];
}
}
return result;
}
// 矩阵求逆
//int** inverseMatrix(int** matrix, int rows, int cols)
//{
// std::cout << "Inverse Matrix:" << std::endl;
// printMatrix(matrix, rows, cols);
// return nullptr;
//}
int** inverseMatrix(int** matrix, int rows, int cols)
{
std::cout << "Inverse Matrix:" << std::endl;
printMatrix(matrix, rows, cols);
return nullptr;
}
void printMatrix(int** matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}
int main()
{
// 创建示例矩阵
int rows = 3;
int cols = 3;
int** matrix = createMatrix(rows, cols);
// 初始化示例矩阵
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
matrix[i][j] = i + j;
}
}
int choice;
do
{
std::cout << "Menu:" << std::endl;
std::cout << "1. Add 2 matrices" << std::endl;
std::cout << "2. Subtract 2 matrices" << std::endl;
std::cout << "3. Multiply 2 matrices" << std::endl;
std::cout << "4. Multiply a matrix by a scalar" << std::endl;
std::cout << "5. Stack 2 matrices vertically" << std::endl;
std::cout << "6. Stack 2 matrices horizontally" << std::endl;
std::cout << "7. Transpose a matrix" << std::endl;
std::cout << "8. Rotate a matrix" << std::endl;
std::cout << "9. Inverse a matrix" << std::endl;
std::cout << "0. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice)
{
case 1:
{ // Add 2 matrices
int** matrix1 = createMatrix(rows, cols);
int** matrix2 = createMatrix(rows, cols);
// 输入矩阵1的值
std::cout << "Enter elements of matrix 1:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix1[i][j];
}
}
// 输入矩阵2的值
std::cout << "Enter elements of matrix 2:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix2[i][j];
}
}
int** result = addMatrix(matrix1, matrix2, rows, cols);
std::cout << "Result:" << std::endl;
printMatrix(result, rows, cols);
// 释放内存
releaseMatrix(matrix1, rows);
releaseMatrix(matrix2, rows);
releaseMatrix(result, rows);
break;
}
case 2:
{
int** matrix3 = createMatrix(rows, cols);
int** matrix4 = createMatrix(rows, cols);
// 输入矩阵3的值
std::cout << "Enter elements of matrix 3:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix3[i][j];
}
}
// 输入矩阵4的值
std::cout << "Enter elements of matrix 4:" << endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix4[i][j];
}
}
int** result2 = subtractMatrix(matrix3, matrix4, rows, cols);
std::cout << "Result:" << std::endl;
printMatrix(result2, rows, cols);
// 释放内存
releaseMatrix(matrix3, rows);
releaseMatrix(matrix4, rows);
releaseMatrix(result2, rows);
break;
}
case 3:
{ // Multiply 2 matrices
int rows1, cols1, cols2;
std::cout << "Enter number of rows in matrix 1: ";
std::cin >> rows1;
std::cout << "Enter number of columns in matrix 1 and rows in matrix 2: ";
std::cin >> cols1;
std::cout << "Enter number of columns in matrix 2: ";
std::cin >> cols2;
int** matrix5 = createMatrix(rows1, cols1);
int** matrix6 = createMatrix(cols1, cols2);
// 输入矩阵1的值
std::cout << "Enter elements of matrix 1:" << std::endl;
for (int i = 0; i < rows1; ++i)
{
for (int j = 0; j < cols1; ++j)
{
std::cin >> matrix5[i][j];
}
}
// 输入矩阵2的值
std::cout << "Enter elements of matrix 2:" << std::endl;
for (int i = 0; i < cols1; ++i)
{
for (int j = 0; j < cols2; ++j)
{
std::cin >> matrix6[i][j];
}
}
int** result3 = multiplyMatrix(matrix5, rows1, cols1, matrix6, cols2);
std::cout << "Result:" << std::endl;
printMatrix(result3, rows1, cols2);
// 释放内存
releaseMatrix(matrix5, rows1);
releaseMatrix(matrix6, cols1);
releaseMatrix(result3, rows1);
break;
}
case 4:
{ // Multiply a matrix by a scalar
int scalar;
std::cout << "Enter scalar value: ";
std::cin >> scalar;
int** matrix7 = createMatrix(rows, cols);
// 输入矩阵的值
std::cout << "Enter elements of the matrix:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix7[i][j];
}
}
int** result4 = multiplyByScalar(matrix7, rows, cols, scalar);
std::cout << "Result:" << std::endl;
printMatrix(result4, rows, cols);
// 释放内存
releaseMatrix(matrix7, rows);
releaseMatrix(result4, rows);
break;
}
case 5:
{ // Stack 2 matrices vertically
int rows2;
std::cout << "Enter number of rows in matrix 2: ";
std::cin >> rows2;
int** matrix8 = createMatrix(rows, cols);
int** matrix9 = createMatrix(rows2, cols);
// 输入矩阵1的值
std::cout << "Enter elements of matrix 1:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix8[i][j];
}
}
// 输入矩阵2的值
std::cout << "Enter elements of matrix 2:" << std::endl;
for (int i = 0; i < rows2; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix9[i][j];
}
}
int** result5 = stackMatrixVertical(matrix8, rows, cols, matrix9, rows2);
std::cout << "Result:" << std::endl;
printMatrix(result5, rows + rows2, cols);
// 释放内存
releaseMatrix(matrix8, rows);
releaseMatrix(matrix9, rows2);
releaseMatrix(result5, rows + rows2);
break;
}
case 6:
{ // Stack 2 matrices horizontally
int cols3;
std::cout << "Enter number of columns in matrix 2: ";
std::cin >> cols3;
int** matrix10 = createMatrix(rows, cols);
int** matrix11 = createMatrix(rows, cols3);
// 输入矩阵1的值
std::cout << "Enter elements of matrix 1:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix10[i][j];
}
}
// 输入矩阵2的值
std::cout << "Enter elements of matrix 2:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols3; ++j)
{
std::cin >> matrix11[i][j];
}
}
int** result6 = stackMatrixHorizontal(matrix10, rows, cols, matrix11, cols3);
std::cout << "Result:" << std::endl;
printMatrix(result6, rows, cols + cols3);
// 释放内存
releaseMatrix(matrix10, rows);
releaseMatrix(matrix11, rows);
releaseMatrix(result6, rows);
break;
}
case 7:
{ // Transpose a matrix
int** matrix12 = createMatrix(rows, cols);
// 输入矩阵的值
std::cout << "Enter elements of the matrix:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix12[i][j];
}
}
int** result7 = transposeMatrix(matrix12, rows, cols);
std::cout << "Result:" << std::endl;
printMatrix(result7, cols, rows);
// 释放内存
releaseMatrix(matrix12, rows);
releaseMatrix(result7, cols);
break;
}
case 8:
{ // Rotate a matrix
int** matrix13 = createMatrix(rows, cols);
// 输入矩阵的值
std::cout << "Enter elements of the matrix:" << std::endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cin >> matrix13[i][j];
}
}
int** result8 = rotateMatrix(matrix13, rows, cols);
std::cout << "Result:" << std::endl;
printMatrix(result8, cols, rows);
// 释放内存
releaseMatrix(matrix13, rows);
releaseMatrix(result8, cols);
break;
}
case 9:
{ // Inverse a matrix
int** matrix14 = createMatrix(rows, cols);
// 输入矩阵的值
std::cout << "Enter elements of the matrix:" << std::endl;
//for (int i = 0; i < rows; ++i)
//{
// for (int j = 0; j < cols; ++j)
// {
// std::cin >> matrix14[i][j];
// }
//}
//int** result9 = inverseMatrix(matrix14, rows, cols);
//// 释放内存
//releaseMatrix(matrix14, rows);
//releaseMatrix(result9, cols);
//break;
int** matrix = new int* [rows];
for (int i = 0; i < rows; i++)
{
matrix[i] = new int[cols];
for (int j = 0; j < cols; j++)
{
matrix[i][j] = i * cols + j + 1;
}
}
// 求逆矩阵
int** invMatrix = inverseMatrix(matrix, rows, cols);
// 回收内存
for (int i = 0; i < rows; i++)
{
delete[] matrix[i];
delete[] invMatrix[i];
}
delete[] matrix;
delete[] invMatrix;
break;
}
case 0:
{// Exit
std::cout << "Exiting..." << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}
}
while (choice != 0);
// 释放示例矩阵内存
releaseMatrix(matrix, rows);
return 0;
}
}
路过的大神请指点一二!做的不好轻点喷!
标签:基本,rows,运算,int,矩阵,cols,++,matrix From: https://www.cnblogs.com/zongmeijiezuishuai/p/17592262.html