当前位置:网站首页>1.C语言矩阵加减法
1.C语言矩阵加减法
2022-07-06 09:19:00 【是王久久阿】
目录
1.原理
矩阵加法需要满足的条件:两个相加的矩阵需要保持维度一致,即矩阵A+B,A的行、列=B的行、列。
加减法原理:矩阵加法的原理很简单,对应位置加减即可。
例如:
2.函数编写(数组形式和指针形式)
因为加减法比较简单,所以只需要利用两个for循环遍历整个数组,将对应位想加减即可,这里用加法举例。
在函数开头需要利用assert断言条件是否成立,如果不成立系统会提示报错,assert需要包含头文件<assert.h>,如果不进行判断,传入错误的参数后,二维数组的访问会溢出。
因为数组传参传的是地址,所以函数的类型设置为void即可。
数组形式:
//定义矩阵的行列
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 3
#include<stdio.h>
#include<assert.h>
//数组形式
void Matrix_addition(double arr1[][COL1], double arr2[][COL2], double arr3[][COL1],int row1, int col1,int row2,int col2)
{
assert(row1 == row2 && col1 == col2);//判断矩阵维数一致
int i = 0;
int j = 0;
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
arr3[i][j] = arr1[i][j] + arr2[i][j];
}
}
}
int main()
{
double arr1[ROW1][COL2] = {0};
double arr2[ROW1][COL2] = {9,8,7,6,5,4,3,2,1};
double arr3[ROW1][COL1] = { 0 };
Matrix_addition(arr1, arr2, arr3, ROW1, COL1,ROW2,COL2);
return 0;
}
指针形式:
//定义矩阵的行列
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 3
#include<stdio.h>
#include<assert.h>
//指针形式
void Matrix_addition(double (*arr1)[COL1], double (*arr2)[COL2], double (*arr3)[COL1], int row1, int col1, int row2, int col2)
{
assert(row1 == row2 && col1 == col2);//判断矩阵维数一致
int i = 0;
int j = 0;
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
*(*(arr3 + i) + j) = *(*(arr1 + i) + j) + *(*(arr2 + i) + j);
}
}
}
int main()
{
double arr1[ROW1][COL2] = {0};
double arr2[ROW1][COL2] = {9,8,7,6,5,4,3,2,1};
double arr3[ROW1][COL1] = { 0 };
Matrix_addition(arr1, arr2, arr3, ROW1, COL1,ROW2,COL2);
return 0;
}
两种形式均可,本质上没有任何差别,根据自己喜好来使用即可。用define定义数组的行列,优点就是灵活,方便使用,后续只需要更改输入矩阵的相关参数即可进行运算。
矩阵乘法相对于加法略有难度,C语言矩阵乘法一文中有详细讲解。
边栏推荐
- 初识C语言(下)
- Application architecture of large live broadcast platform
- arduino+水位传感器+led显示+蜂鸣器报警
- [Topic terminator]
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
- 一文搞定 UDP 和 TCP 高频面试题!
- One article to get UDP and TCP high-frequency interview questions!
- 架构师怎样绘制系统架构蓝图?
- Inheritance and polymorphism (I)
- 系统设计学习(一)Design Pastebin.com (or Bit.ly)
猜你喜欢
Music playback (toggle & playerprefs)
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
Small exercise of library management system
9.指针(上)
继承和多态(上)
Experience summary of autumn recruitment of state-owned enterprises
Smart classroom solution and mobile teaching concept description
Record: the solution of MySQL denial of access when CMD starts for the first time
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
随机推荐
Record: the solution of MySQL denial of access when CMD starts for the first time
Employment of cashier [differential constraint]
西安电子科技大学22学年上学期《基础实验》试题及答案
TYUT太原理工大学2022软工导论大题汇总
The earth revolves around the sun
架构师怎样绘制系统架构蓝图?
记录:初次cmd启动MySQL拒接访问之解决
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
List set map queue deque stack
最新坦克大战2022-全程开发笔记-2
Smart classroom solution and mobile teaching concept description
IPv6 experiment
Atomic and nonatomic
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
Interview Essentials: talk about the various implementations of distributed locks!
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
MPLS experiment
Chromatic judgement bipartite graph
IPv6 experiment