# 两条对角线之间的矩阵元素

#### Matrix elements between two diagonals

``````#include <stdio.h>

int main() {
int i, j, n = 5, x = 4, y = 6;
int mat[100][100];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
if (i == j || i + j >= n - 1 || j < i) mat[i][j] = y;
else mat[i][j] = x;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}
``````

OUTPUT：

6 4 4 4 6

6 6 4 6 6

6 6 6 6 6

6 6 6 6 6

6 6 6 6 6

2 个回复

``````#include <stdio.h>
#include <sys/param.h>

int main() {
int i, j, n = 5, x = 4, y = 6;
int mat[n][n];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
int d1 = i;
int d2 = n - i - 1;
if (j > MIN(d1, d2) && j < MAX(d1, d2)) mat[i][j] = x;
else mat[i][j] = y;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}
``````

``````#include <stdio.h>

int main() {
int i, j, n = 5, x = 4, y = 6;
int mat[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j > i && j < n - i - 1) {
/* upper triangle */
mat[i][j] = x;
} else
if (j < i && j > n - i - 1) {
/* lower triangle */
mat[i][j] = x;
} else {
/* everywhere else */
mat[i][j] = y;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}
``````
1 R中对角线之间的矩阵元素之和

2 获取矩阵的两个对角线

3 在2D矩阵中找到给定点相交的两个对角线

5 计算矩阵中的空对角线

6 c 中具有矩阵和对角线的任务

7 计算矩阵中对角线的总和

8 获取此矩阵的对角线

9 在对角线上重新排列矩阵 (JS)

2021-04-26 01:08:03 0 32   arrays
10 将矩阵对角线转换为列

© 2020-2022 STACKOOM.COM