当前位置:网站首页>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语言矩阵乘法一文中有详细讲解。
边栏推荐
- First acquaintance with C language (Part 1)
- [Topic terminator]
- System design learning (I) design pastebin com (or Bit.ly)
- MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
- String类
- Iterable、Collection、List 的常见方法签名以及含义
- Common method signatures and meanings of Iterable, collection and list
- 图书管理系统小练习
- 学编程的八大电脑操作,总有一款你不会
- MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
猜你喜欢
![Fundamentals of UD decomposition of KF UD decomposition [1]](/img/e9/564e0163c3756c0ba886913f1cfaef.jpg)
Fundamentals of UD decomposition of KF UD decomposition [1]

View UI plus released version 1.2.0 and added image, skeleton and typography components

图书管理系统小练习

Data manipulation language (DML)

Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction

几道高频的JVM面试题

TYUT太原理工大学2022软工导论大题汇总

Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing

Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited

Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
随机推荐
12 excel charts and arrays
View UI plus released version 1.2.0 and added image, skeleton and typography components
Role movement in the first person perspective
Iterable、Collection、List 的常见方法签名以及含义
错误:排序与角标越界
Fgui project packaging and Publishing & importing unity & the way to display the UI
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
Common method signatures and meanings of Iterable, collection and list
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
2-year experience summary, tell you how to do a good job in project management
国企秋招经验总结
What are the advantages of using SQL in Excel VBA
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
Introduction pointer notes
Record: solution of 404 error of servlet accessing database in dynamic web project
阿里云微服务(四) Service Mesh综述以及实例Istio
TYUT太原理工大学2022数据库考试题型大纲
XV Function definition and call
继承和多态(上)