当前位置:网站首页>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语言矩阵乘法一文中有详细讲解。
边栏推荐
- Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- 初识C语言(下)
- MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
- Inheritance and polymorphism (Part 2)
- [Topic terminator]
- Music playback (toggle & playerprefs)
- Fairygui bar subfamily (scroll bar, slider, progress bar)
- 121 distributed interview questions and answers
- Introduction pointer notes
猜你喜欢
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
Rt-ppp test using rtknavi
Redis介绍与使用
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
Design a key value cache to save the results of the most recent Web server queries
学编程的八大电脑操作,总有一款你不会
Abstract classes and interfaces
Role movement in the first person perspective
121 distributed interview questions and answers
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
随机推荐
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
Edit distance (multi-source BFS)
阿里云微服务(四) Service Mesh综述以及实例Istio
Cloud native trend in 2022
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
167. Sum of two numbers II - input ordered array - Double pointers
Fundamentals of UD decomposition of KF UD decomposition [1]
FileInputStream和BufferedInputStream的比较
Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
Application architecture of large live broadcast platform
Arduino+ water level sensor +led display + buzzer alarm
Tyut outline of 2022 database examination of Taiyuan University of Technology
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
记录:初次cmd启动MySQL拒接访问之解决
[Topic terminator]
继承和多态(上)
Fairygui bar subfamily (scroll bar, slider, progress bar)
162. Find peak - binary search
西安电子科技大学22学年上学期《基础实验》试题及答案
阿里云一面:并发场景下的底层细节 - 伪共享问题