图像中的计算往往是矩阵之间的运算,所以矩阵乘法具有重要的意义。
矩阵乘法
C代码
#include<stdio.h>
#include<stdlib.h>
int main() {
int arr1[3][4] = { 0 };
int arr2[4][3] = { 0 };
int arr3[3][3] = { 0 };
printf("输入第一个矩阵\n");
for (int i = 0; i <3; i++)
{
printf("输入第%d行:",i+1);
for (int j = 0; j < 4; j++)
{
scanf_s("%d", &arr1[i][j]);
}
printf("\n");
}
printf("输入第二个矩阵\n");
for (int i = 0; i < 4; i++)
{
printf("输入第%d行:", i + 1);
for (int j = 0; j < 3; j++)
{
scanf_s("%d", &arr2[i][j]);
}
printf("\n");
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int sum = 0;
for (int k = 0; k < 4; k++)
{
sum = sum + arr1[i][k] * arr2[k][j];
}
arr3[i][j] = sum;
}
}
printf("矩阵相乘为:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%4d",arr3[i][j]);
}
printf("\n");
}
}
矩阵的点乘:对应元素相乘
代码
#include<stdio.h>
#include<stdlib.h>
#define row 3
#define col 4
int main() {
int arr1[row][col] = { 0 };
int arr2[row][col] = { 0 };
int arr3[row][col] = { 0 };
printf("输入第一个矩阵\n");
for (int i = 0; i < row; i++)
{
printf("输入第%d行:",i+1);
for (int j = 0; j < col; j++)
{
scanf_s("%d", &arr1[i][j]);
}
printf("\n");
}
printf("第二个矩阵为\n");
for (int i = 0; i < row; i++)
{
printf("输入第%d行:", i + 1);
for (int j = 0; j < col; j++)
{
scanf_s("%d", &arr2[i][j]);
}
printf("\n");
}
printf("矩阵点乘的结果为:\n");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("%4d", arr2[i][j]*arr1[i][j]);
}
printf("\n");
}
return 0;
}