当前位置:网站首页>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语言矩阵乘法一文中有详细讲解。
边栏推荐
- Error: symbol not found
- Atomic and nonatomic
- Rich Shenzhen people and renting Shenzhen people
- Interview Essentials: talk about the various implementations of distributed locks!
- [while your roommate plays games, let's see a problem]
- What are the advantages of using SQL in Excel VBA
- 几道高频的JVM面试题
- 4.30动态内存分配笔记
- 系统设计学习(三)Design Amazon‘s sales rank by category feature
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
猜你喜欢

西安电子科技大学22学年上学期《信号与系统》试题及答案

Quickly generate illustrations

MPLS experiment

Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology

学编程的八大电脑操作,总有一款你不会

Redis介绍与使用
![Heap sort [handwritten small root heap]](/img/f0/6efda3c6f499a32671a935dd2f21db.png)
Heap sort [handwritten small root heap]

Fgui project packaging and Publishing & importing unity & the way to display the UI

Dark chain lock (lca+ difference on tree)

(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
随机推荐
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
Network layer 7 protocol
View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
[while your roommate plays games, let's see a problem]
面试必备:聊聊分布式锁的多种实现!
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
几道高频的JVM面试题
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
First acquaintance with C language (Part 1)
一文搞定 UDP 和 TCP 高频面试题!
13 power map
Redis介绍与使用
Several high-frequency JVM interview questions
Relational algebra of tyut Taiyuan University of technology 2022 database
Small exercise of library management system
Alibaba cloud microservices (IV) service mesh overview and instance istio
Rt-ppp test using rtknavi
TYUT太原理工大学2022软工导论简答题
记录:初次cmd启动MySQL拒接访问之解决
Abstract classes and interfaces