提问人:Comp.Sci.Anon 提问时间:10/21/2023 最后编辑:Comp.Sci.Anon 更新时间:11/7/2023 访问量:39
Java 编码马尔可夫矩阵
Java coding Markov Matrix
问:
日安!
我当前的代码有问题。我正在尝试编写一个带有标头的方法 公共静态布尔值 isMarkovMatrix(double[][] m) 这应该检查矩阵是否为正马尔可夫矩阵。 然后在 main 方法中,我尝试提示用户输入一个 3×3 的双精度值矩阵,然后测试输入的矩阵是否实际上是正马尔可夫矩阵。 更清楚一点 - 正马尔可夫矩阵是一个矩阵,其中每个元素都是正的,每列中元素的总和为 1。
这是我当前的代码,无论我输入什么,它都会继续告诉我,当我知道它不是一个积极的 M.M 时, 我对编码还很陌生,到目前为止只上了两门课程,所以我不知道任何太高级的东西,谢谢!希望有人能帮忙...
public static void main(String[] args) {
Scanner kbd = new Scanner (System.in);
double[][] matrix = new double [3][3];
System.out.println("Enter a 3-by-3 matrix row by row: ");
for(int row = 0; row < 3; row++){
for(int column = 0; column < 3; column++){
matrix[row][column] = kbd.nextDouble();
}
kbd.nextLine();
}
//output
if(isMarkovMatrix(matrix)){
System.out.println("It is a Markov Matrix.");
} else if (!isMarkovMatrix(matrix)){
System.out.println("It is not a Markov Matrix");
}
}
public static boolean isMarkovMatrix(double[][] m){
double sum;
boolean answer = true;
for(int row = 0; row < m.length; row++){
sum = 0;
for(int column = 0; column < m.length; column++){
sum += m[row][column];
if (sum != 1 || m[row][column] < 0){
answer = false;
}
}
}
return answer;
}
答:
0赞
Idle_Mind
10/21/2023
#1
您当前的代码正在检查 ROW 的总和,而不是列的总和。此外,您需要在退出嵌套的 for 循环后检查总和是否为 1;否则,在你有机会将所有数字相加之前,你将确定它“不是”一个有效的矩阵。
因此,您需要交换 for 循环中行/列的顺序,并将总和 1 的检查移动到嵌套的 for 循环之后。
这看起来更像是:
public static boolean isMarkovMatrix(double[][] m){
double sum;
for(int column = 0; column < m.length; column++){
sum = 0;
for(int row = 0; row < m.length; row++){
sum += m[row][column];
if (m[row][column] < 0){
return false;
}
}
if (sum != 1.0) {
return false;
}
}
return true;
}
评论
0赞
Comp.Sci.Anon
10/21/2023
哇扎,这么简单的改变,但它完全有效,而且完全有意义!非常感谢!
0赞
Reilas
10/21/2023
#2
"...它继续告诉我这不是一个积极的 M.M......”
我无法破译 isMarkovMatrix 方法的逻辑。
这是一个参考;不是解决方案。
double e = 0;
for (int c = 0, m = a[0].length; c < m; c++, e = 0) {
for (double[] r : a) e += r[c];
if (e != 1) return false;
}
return true;
这里的问题是,在添加浮点值时会遇到精度错误。
请改用 BigDecimal 类。
BigDecimal e = BigDecimal.ZERO;
for (int c = 0, n = m[0].length; c < n; c++, e = BigDecimal.ZERO) {
for (double[] r : m)
e = e.add(new BigDecimal(String.valueOf(r[c])));
if (e.compareTo(BigDecimal.ONE) != 0) return false;
}
return true;
输出
Enter a 3-by-3 matrix row by row:
.1 .2 .3
.4 .5 .6
.5 .3 .1
It is a Markov Matrix.
0赞
school GAS
11/7/2023
#3
import java.util.*;
public class Markov
{
Scanner sc=new Scanner(System.in);
double m[][];
void main()
{
System.out.println("Enter order of matrix");
int n=sc.nextInt();
int b=0;
while(!(n>=3&&n<=9))
{
System.out.println("ERROR the value is out of bound");
System.out.println("Re-enter");
int x=sc.nextInt();
n=x;
}
m=new double[n][n];
for(int i=0;i<m.length;i++)
{
for(int j=0;j<m.length;j++)
{
m[i][j]=sc.nextDouble();
while(m[i][j]<0)
{
System.out.println("ERROR negative value entered");
System.out.println("Re-enter");
int x=sc.nextInt();
m[i][j]=x;
}
}
}
int a=0;
for(int i=0;i<m.length;i++)
{
for(int j=0;j<m.length;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(int r=0;r<n;r++)
{ double s1=0.0;double s2=0.0;
for(int c=0;c<n;c++)
{
s1+=m[r][c];
s2+=m[c][r];
}
if(s1!=1||s2!=1)
a++;
}
if(a>0)
System.out.println("It is not a doubly markov matrix");
else
System.out.println("It is a doubly markov number");
}
}
评论