[英]multiply diagonal values of a matrix in java
I want to multiply 2 diagonals of a Matrix. 我想乘以一个矩阵的2个对角线。 But i am not able to get the diagonals of matrix.
但是我无法获得矩阵的对角线。 like in given code two diagonals are
d1=5*5*9
. 就像给定代码中的两个对角线是
d1=5*5*9
。 and d2=3*5*7
then i will use d1 and d2 values for further process. 和
d2=3*5*7
那么我将使用d1和d2值进行进一步处理。
How to do it. 怎么做。 Note: matrix size can be different like here its 3x3 but it can be 5x5
注意:矩阵大小可以不同,例如此处为3x3,但可以为5x5
private static int diagonalMultiply(int [][]arr1){
int[][] arr= {
{5,2,3},
{4,5,6},
{7,8,9}
};
for ( int x = 0; x < arr.length; x++) //stepping along the x axis again.
{
for ( int y = 0; y < arr[x].length; y++) // stepping along the y axis.
{
System.out.print(arr[x][y]+" ");
}
}
return 0;
}
A diagonal of an N×N matrix has N elements. N×N矩阵的对角线具有N个元素。 A pair of nested loops, each going from
0
to N-1
, cover N 2 elements. 一对嵌套循环,每个循环从
0
到N-1
,覆盖N 2个元素。 This means that you need one loop, not two. 这意味着您需要一个循环,而不是两个。
Both diagonals can be retrieved in a single loop. 两个对角线都可以在单个循环中检索。 Indexes of the descending diagonal are
(i, i)
, while indexes of the ascending one are (Ni-1, i)
: 对角降序的索引是
(i, i)
,而升斜对角的索引是(Ni-1, i)
:
int N = arr.length;
for ( int i = 0; i < N ; i++) {
System.out.println(arr[i][i]+" "+arr[N-i-1][i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.