当前位置:网站首页>1. C language matrix addition and subtraction method
1. C language matrix addition and subtraction method
2022-07-06 13:26:00 【It's Wang Jiujiu】
Catalog
2. Function writing ( Array form and pointer form )
1. principle
The conditions that matrix addition needs to meet : Two added matrices need to keep the same dimension , Matrix A+B,A The line of 、 Column =B The line of 、 Column .
Principle of addition and subtraction : The principle of matrix addition is very simple , Add or subtract the corresponding position .
for example :

2. Function writing ( Array form and pointer form )
Because the addition and subtraction method is relatively simple , So just use two for Loop through the entire array , Add or subtract the corresponding bit , Here is an example of addition .
At the beginning of the function, you need to use assert Assert whether the condition holds , If not, the system will prompt an error ,assert Need to include header file <assert.h>, If there is no judgment , After passing in the wrong parameter , The access of two-dimensional array will overflow .
Because the array parameter passes the address , So the type of the function is set to void that will do .
Array form :
// Define the rows and columns of the matrix
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 3
#include<stdio.h>
#include<assert.h>
// Array form
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);// The dimension of the judgment matrix is consistent
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;
}Pointer form :
// Define the rows and columns of the matrix
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 3
#include<stdio.h>
#include<assert.h>
// Pointer form
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);// The dimension of the judgment matrix is consistent
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;
}Both forms can , There's no difference in nature , Use it according to your preferences . use define Define the rows and columns of the array , The advantage is flexibility , Easy to use , Subsequently, you only need to change the relevant parameters of the input matrix to perform the operation .
Matrix multiplication is slightly more difficult than addition ,C Linguistic matrix multiplication There is a detailed explanation of .
边栏推荐
- Solution: warning:tensorflow:gradients do not exist for variables ['deny_1/kernel:0', 'deny_1/bias:0',
- Answer to "software testing" exercise: Chapter 1
- CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
- 一文搞定 UDP 和 TCP 高频面试题!
- 9.指针(上)
- Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
- 六种集合的遍历方式总结(List Set Map Queue Deque Stack)
- Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
- TYUT太原理工大学2022数据库大题之E-R图转关系模式
猜你喜欢

2.C语言矩阵乘法

MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景

Inheritance and polymorphism (Part 2)

TYUT太原理工大学2022“mao gai”必背

3.输入和输出函数(printf、scanf、getchar和putchar)

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

TYUT太原理工大学2022数据库大题之分解关系模式

1.C语言初阶练习题(1)

(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

How do architects draw system architecture blueprints?
随机推荐
Set container
阿里云微服务(三)Sentinel开源流控熔断降级组件
JS interview questions (I)
最新坦克大战2022-全程开发笔记-1
[中国近代史] 第六章测验
TYUT太原理工大学2022数据库题库选择题总结
12 excel charts and arrays
167. Sum of two numbers II - input ordered array - Double pointers
更改VS主题及设置背景图片
Branch and loop statements
Network layer 7 protocol
View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
用栈实现队列
Abstract classes and interfaces
First acquaintance with C language (Part 1)
View UI plus released version 1.3.0, adding space and $imagepreview components
Record: newinstance() obsolete replacement method
系统设计学习(一)Design Pastebin.com (or Bit.ly)
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology