Return不考虑对角系数的矩阵的一行的最大值
Return the maximum value of a row of a matrix without considering the diagonal coefficients
我有一个二维数组:
0. | 0.0 | -0.8980387129971331 | -0.8900869793075101 | -0.8906789098245378 | 1.0104911316104093 | -0.8816392828513628
1. | -0.8998803800424156 | 0.0 | -0.8894871457733221 | -0.8897044897987794 | 1.1079409359304297 | -0.7105118305961893
2. | -0.8889556072705933 | -0.8924868056899387 | 0.0 | 1.1083728720261286 | 1.0098247893112775 | 1.099113864022297
3. | -0.8808751963282109 | 0.9280169284175466 | -0.8891630366886065 | 0.0 | -0.69121432906078 | -0.7092216479617963
4. | -0.8986589499572509 | -0.8921590617526629 | -0.8891630366344203 | -0.7057342552186525 | 0.0 | -0.7075934709028173
5. | -0.8988751964282238 | -0.8981045503211356 | -0.8891659511135326 | 1.0907466603012215 | 1.1072644730546006 | 0.0
我想得到 the maximum of a line
而不考虑 diagonal coefficients
.
我正在使用以下功能:
public static int max(double[] array) {
int max = 0;
double old = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > old) {
max = i;
old = array[i];
}
}
return max;
}
但是例如对于 4th line
它 return 是我的值 0.0
而我希望它 return: -0.7057342552186525
.
必须returnall the values
中的maximum
除了value of the column 4
.
编辑
public static int max2(double[][] array ,int k) {
int max = Integer.MIN_VALUE;
double old = array[0][0];
for(int j =0 ; j<array.length; j++ ) {
if(k==j) {
continue;
}
if (array[k][j] > old) {
max = j;
old = array[k][j];
}
}
return max;
}
我遇到了这个错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648
- 你return一个索引而不是元素
- 你 return 一个 int 而不是 double
- 你从 [1]
开始一个数组
您似乎想在二维数组的每一行上调用 max()。
所以这就是我们遇到麻烦的原因:你不知道哪个元素是对角系数。如果在你的任务中你绝对确定除了对角系数之外没有元素等于 0.0,你可以使用下一个代码,调用 max(yourTwoDimensionalArray[lineToFindTheMaximumIn]):
public static double max(double[] array) {
double max = Double.NEGATIVE_INFINITY;
for(int i = 0; i < array.length; i++){
if(array[i]!=0) max = Double.max(max, array[i]);
}
return max;
}
另请注意,数组从 [0] 开始,而不是从 [1]
如您所见,max(yourArray[3]) returns -0.7057342552186525 是数组中的第 4 行。
希望对您有所帮助,如果没有,请发表评论,我会尽力向您解释更多
如果允许Java流API,可以这样找到double数组中的最大值(不包括该行的索引):
public static Double max(double[] row, int rowId) {
return IntStream
.range(0, row.length) // analog of for-loop
.filter(i -> i != rowId) // skip "diagonal" index
.mapToDouble(i -> row[i]) // get value in the row by index
.max() // find max OptionalDouble
.getAsDouble(); // get double value
}
类似的非流版本如下所示(return null
如果没有最大值可用):
public static Double maxWithLoop(double[] row, int rowId) {
Double max = null;
for (int i = 0; i < row.length; i++) {
if (i == rowId) {
continue;
}
if (null == max || max < row[i]) {
max = row[i];
}
}
return max;
}
测试
double[][] data = {
{ 0.0, -0.8980387129971331, -0.8900869793075101, -0.8906789098245378, 1.0104911316104093, -0.8816392828513628},
{ -0.8998803800424156, 0.0, -0.8894871457733221, -0.8897044897987794, 1.1079409359304297, -0.7105118305961893},
{ -0.8889556072705933, -0.8924868056899387, 0.0, 1.1083728720261286, 1.0098247893112775, 1.099113864022297},
{ -0.8808751963282109, 0.9280169284175466, -0.8891630366886065, 0.0, -0.69121432906078, -0.7092216479617963},
{ -0.8986589499572509, -0.8921590617526629, -0.8891630366344203, -0.7057342552186525, 0.0, -0.7075934709028173},
{ -0.8988751964282238, -0.8981045503211356, -0.8891659511135326, 1.0907466603012215, 1.1072644730546006, 0.0}
};
int i = 0;
for (double[] row : data) {
System.out.printf("max in row #%d: % .4f%n", i, max(row, i++));
}
输出
max in row #0: 1.0105
max in row #1: 1.1079
max in row #2: 1.1084
max in row #3: 0.9280
max in row #4: -0.7057
max in row #5: 1.1073
我有一个二维数组:
0. | 0.0 | -0.8980387129971331 | -0.8900869793075101 | -0.8906789098245378 | 1.0104911316104093 | -0.8816392828513628
1. | -0.8998803800424156 | 0.0 | -0.8894871457733221 | -0.8897044897987794 | 1.1079409359304297 | -0.7105118305961893
2. | -0.8889556072705933 | -0.8924868056899387 | 0.0 | 1.1083728720261286 | 1.0098247893112775 | 1.099113864022297
3. | -0.8808751963282109 | 0.9280169284175466 | -0.8891630366886065 | 0.0 | -0.69121432906078 | -0.7092216479617963
4. | -0.8986589499572509 | -0.8921590617526629 | -0.8891630366344203 | -0.7057342552186525 | 0.0 | -0.7075934709028173
5. | -0.8988751964282238 | -0.8981045503211356 | -0.8891659511135326 | 1.0907466603012215 | 1.1072644730546006 | 0.0
我想得到 the maximum of a line
而不考虑 diagonal coefficients
.
我正在使用以下功能:
public static int max(double[] array) {
int max = 0;
double old = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > old) {
max = i;
old = array[i];
}
}
return max;
}
但是例如对于 4th line
它 return 是我的值 0.0
而我希望它 return: -0.7057342552186525
.
必须returnall the values
中的maximum
除了value of the column 4
.
编辑
public static int max2(double[][] array ,int k) {
int max = Integer.MIN_VALUE;
double old = array[0][0];
for(int j =0 ; j<array.length; j++ ) {
if(k==j) {
continue;
}
if (array[k][j] > old) {
max = j;
old = array[k][j];
}
}
return max;
}
我遇到了这个错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648
- 你return一个索引而不是元素
- 你 return 一个 int 而不是 double
- 你从 [1] 开始一个数组
您似乎想在二维数组的每一行上调用 max()。 所以这就是我们遇到麻烦的原因:你不知道哪个元素是对角系数。如果在你的任务中你绝对确定除了对角系数之外没有元素等于 0.0,你可以使用下一个代码,调用 max(yourTwoDimensionalArray[lineToFindTheMaximumIn]):
public static double max(double[] array) {
double max = Double.NEGATIVE_INFINITY;
for(int i = 0; i < array.length; i++){
if(array[i]!=0) max = Double.max(max, array[i]);
}
return max;
}
另请注意,数组从 [0] 开始,而不是从 [1]
如您所见,max(yourArray[3]) returns -0.7057342552186525 是数组中的第 4 行。
希望对您有所帮助,如果没有,请发表评论,我会尽力向您解释更多
如果允许Java流API,可以这样找到double数组中的最大值(不包括该行的索引):
public static Double max(double[] row, int rowId) {
return IntStream
.range(0, row.length) // analog of for-loop
.filter(i -> i != rowId) // skip "diagonal" index
.mapToDouble(i -> row[i]) // get value in the row by index
.max() // find max OptionalDouble
.getAsDouble(); // get double value
}
类似的非流版本如下所示(return null
如果没有最大值可用):
public static Double maxWithLoop(double[] row, int rowId) {
Double max = null;
for (int i = 0; i < row.length; i++) {
if (i == rowId) {
continue;
}
if (null == max || max < row[i]) {
max = row[i];
}
}
return max;
}
测试
double[][] data = {
{ 0.0, -0.8980387129971331, -0.8900869793075101, -0.8906789098245378, 1.0104911316104093, -0.8816392828513628},
{ -0.8998803800424156, 0.0, -0.8894871457733221, -0.8897044897987794, 1.1079409359304297, -0.7105118305961893},
{ -0.8889556072705933, -0.8924868056899387, 0.0, 1.1083728720261286, 1.0098247893112775, 1.099113864022297},
{ -0.8808751963282109, 0.9280169284175466, -0.8891630366886065, 0.0, -0.69121432906078, -0.7092216479617963},
{ -0.8986589499572509, -0.8921590617526629, -0.8891630366344203, -0.7057342552186525, 0.0, -0.7075934709028173},
{ -0.8988751964282238, -0.8981045503211356, -0.8891659511135326, 1.0907466603012215, 1.1072644730546006, 0.0}
};
int i = 0;
for (double[] row : data) {
System.out.printf("max in row #%d: % .4f%n", i, max(row, i++));
}
输出
max in row #0: 1.0105
max in row #1: 1.1079
max in row #2: 1.1084
max in row #3: 0.9280
max in row #4: -0.7057
max in row #5: 1.1073