c语言大矩阵提取子矩阵
时间: 2023-11-30 20:02:07 浏览: 42
好的,以下是一个用C语言实现的提取大矩阵中的子矩阵的示例代码:
```c
#include <stdio.h>
void extractSubmatrix(int matrix[][4], int submatrix[][2], int row, int col, int subRow, int subCol) {
for (int i = 0; i < subRow; i++) {
for (int j = 0; j < subCol; j++) {
submatrix[i][j] = matrix[row+i][col+j];
}
}
}
int main() {
int matrix[4][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int submatrix[2][2];
extractSubmatrix(matrix, submatrix, 1, 1, 2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", submatrix[i][j]);
}
printf("\n");
}
return 0;
}
```
这个示例代码接受一个大矩阵和一个小矩阵的行列数以及子矩阵左上角在大矩阵中的行列数作为输入,并将子矩阵提取到一个新的二维数组中。在这个示例中,大矩阵是一个4x4的矩阵,子矩阵是一个2x2的矩阵,且子矩阵的左上角在大矩阵的第二行第二列。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)