C语言输入两个矩阵并实现矩阵的加法、乘法、转置并计算协方差矩阵
时间: 2023-12-20 07:07:43 浏览: 93
注:本题解使用了动态内存分配,需要在编译时加上参数“-std=c99”
```c
#include <stdio.h>
#include <stdlib.h>
// 矩阵结构体
typedef struct {
int row;
int col;
double **data;
} Matrix;
// 创建矩阵
Matrix createMatrix(int row, int col) {
Matrix mat;
mat.row = row;
mat.col = col;
mat.data = (double **)malloc(row * sizeof(double *));
for (int i = 0; i < row; i++) {
mat.data[i] = (double *)malloc(col * sizeof(double));
}
return mat;
}
// 释放矩阵
void freeMatrix(Matrix mat) {
for (int i = 0; i < mat.row; i++) {
free(mat.data[i]);
}
free(mat.data);
}
// 读取矩阵
void readMatrix(Matrix *mat) {
for (int i = 0; i < mat->row; i++) {
for (int j = 0; j < mat->col; j++) {
scanf("%lf", &mat->data[i][j]);
}
}
}
// 打印矩阵
void printMatrix(Matrix mat) {
for (int i = 0; i < mat.row; i++) {
for (int j = 0; j < mat.col; j++) {
printf("%.2lf ", mat.data[i][j]);
}
printf("\n");
}
}
// 矩阵加法
Matrix addMatrix(Matrix mat1, Matrix mat2) {
Matrix mat = createMatrix(mat1.row, mat1.col);
for (int i = 0; i < mat1.row; i++) {
for (int j = 0; j < mat1.col; j++) {
mat.data[i][j] = mat1.data[i][j] + mat2.data[i][j];
}
}
return mat;
}
// 矩阵乘法
Matrix mulMatrix(Matrix mat1, Matrix mat2) {
Matrix mat = createMatrix(mat1.row, mat2.col);
for (int i = 0; i < mat1.row; i++) {
for (int j = 0; j < mat2.col; j++) {
double sum = 0;
for (int k = 0; k < mat1.col; k++) {
sum += mat1.data[i][k] * mat2.data[k][j];
}
mat.data[i][j] = sum;
}
}
return mat;
}
// 矩阵转置
Matrix transpose(Matrix mat) {
Matrix tm = createMatrix(mat.col, mat.row);
for (int i = 0; i < mat.row; i++) {
for (int j = 0; j < mat.col; j++) {
tm.data[j][i] = mat.data[i][j];
}
}
return tm;
}
// 协方差矩阵
Matrix covMatrix(Matrix mat) {
Matrix tm = transpose(mat);
Matrix mean = createMatrix(1, mat.col);
for (int i = 0; i < mat.col; i++) {
mean.data[0][i] = 0;
for (int j = 0; j < mat.row; j++) {
mean.data[0][i] += mat.data[j][i];
}
mean.data[0][i] /= mat.row;
}
Matrix cov = createMatrix(mat.col, mat.col);
for (int i = 0; i < mat.col; i++) {
for (int j = 0; j < mat.col; j++) {
double sum = 0;
for (int k = 0; k < mat.row; k++) {
sum += (mat.data[k][i] - mean.data[0][i]) * (tm.data[j][k] - mean.data[0][j]);
}
cov.data[i][j] = sum / (mat.row - 1);
}
}
freeMatrix(tm);
freeMatrix(mean);
return cov;
}
int main() {
int m, n;
printf("请输入矩阵1的行数和列数:");
scanf("%d %d", &m, &n);
Matrix mat1 = createMatrix(m, n);
printf("请输入矩阵1的元素:\n");
readMatrix(&mat1);
printf("请输入矩阵2的行数和列数:");
scanf("%d %d", &m, &n);
Matrix mat2 = createMatrix(m, n);
printf("请输入矩阵2的元素:\n");
readMatrix(&mat2);
printf("矩阵1的元素为:\n");
printMatrix(mat1);
printf("矩阵2的元素为:\n");
printMatrix(mat2);
Matrix mat = addMatrix(mat1, mat2);
printf("矩阵1和矩阵2的和为:\n");
printMatrix(mat);
freeMatrix(mat);
mat = mulMatrix(mat1, mat2);
printf("矩阵1和矩阵2的积为:\n");
printMatrix(mat);
freeMatrix(mat);
mat = transpose(mat1);
printf("矩阵1的转置为:\n");
printMatrix(mat);
freeMatrix(mat);
mat = covMatrix(mat1);
printf("矩阵1的协方差矩阵为:\n");
printMatrix(mat);
freeMatrix(mat);
freeMatrix(mat1);
freeMatrix(mat2);
return 0;
}
```
阅读全文