Java цепь Маркова - PullRequest
       93

Java цепь Маркова

0 голосов
/ 11 июля 2020
• 1000 значение P. Это прямой вывод «Марковская цепь недопустимая строка, не равная 1» и завершается. Кто-нибудь может помочь мне это исправить, пожалуйста. Заранее спасибо.
public class assgmatrix {
    
    static void printArray(double arrayfreq[]) {
        
        for(double i:arrayfreq) {
            System.out.println(i);
            
        }
    }
    
    static void printArrayM(double arrayfreq[][]) {
        System.out.println();
        System.out.println(Arrays.deepToString(arrayfreq).replace("]","\n").replace(",","\t|").replaceAll("[\\[\\]]"," "));
        System.out.println();
        
    }
    
    static boolean checkMark(double m[][]) {
        
        for(int i=0; i<m.length; i++) {
            double sum=0;
            for(int j=0; j<m[i].length; j++)
                sum+=m[i][j];
            
            if(sum !=1)
                return false;
        }
        return true;
    }
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        System.out.println(" Matrix 3 x 3 ");
        System.out.println();
        System.out.println(" Enter The Value for Row 1 Colum 1 :");
        double oneM=input.nextDouble();
        System.out.println(" Enter The Value for Row 1 Colum 2 :");
        double twoM=input.nextDouble();
        System.out.println(" Enter The Value for Row 1 Colum 3 :");
        double threeM=input.nextDouble();
        System.out.println();
        System.out.println(" Enter The Value for Row 2 Colum 1 :");
        double fourM=input.nextDouble();
        System.out.println(" Enter The Value for Row 2 Colum 2 :");
        double fiveM=input.nextDouble();
        System.out.println(" Enter The Value for Row 2 Colum 3 :");
        double sixM=input.nextDouble();
        System.out.println();
        System.out.println(" Enter The Value for Row 3 Colum 1 :");
        double sevenM=input.nextDouble();
        System.out.println(" Enter The Value for Row 3 Colum 2 :");
        double eightM=input.nextDouble();
        System.out.println(" Enter The Value for Row 3 Colum 3 :");
        double nineM=input.nextDouble();
        System.out.println();
        
        double m[][]= { { oneM, twoM, threeM, fourM, fiveM, sixM, sevenM, eightM, nineM} };
        
        double firstinput=0.0;
        double secondinput=0.0;
        double thirdinput=0.0;
        double checkSum=0.0;
        
        if(checkMark(m)) {
            System.out.println(" Valid Markox Matrix as Sum for each Row is Equal to 1 ");
            System.out.println(" Enter First P Value in % :");
            firstinput=input.nextDouble();
            firstinput=firstinput/100;
            
            System.out.println(" Enter Second P Value in % :");
            secondinput=input.nextDouble();
            secondinput=secondinput/100;
            
            System.out.println(" Enter Third P Value in % :");
            thirdinput=input.nextDouble();
            thirdinput=thirdinput/100;
            checkSum=firstinput+secondinput+thirdinput;
            
            if(checkSum==1) {
                System.out.println("Transition Of Matrix Is : ");
                printArrayM(m);
                
                System.out.println("initial Probibility Vector that had been Entered is :");
                System.out.println(" "+firstinput+","+secondinput+","+thirdinput);
                System.out.println();
                System.out.println(" ");
                
                double firstRow=(firstinput*m[0][0])+(secondinput*m[1][0])+(thirdinput*m[2][0]);
                double secondRow=(firstinput*m[0][1])+(secondinput*m[1][1])+(thirdinput*m[2][1]);
                double thirdRow=(firstinput*m[0][2])+(secondinput*m[1][2])+(thirdinput*m[2][2]);
                double sum = firstRow+secondRow+thirdRow;
                
                String s1=String.format("%.3f", firstRow);
                String s2=String.format("%.3f", secondRow);
                String s3=String.format("%.3f", thirdRow);
                
                System.out.println(" The % that been Entered for First Row :"+firstinput+"First Column :"+s1);
                System.out.println(" The % that been Entered for Second Row :"+secondinput+"First Column :"+s2);
                System.out.println(" The % that been Entered for Third Row :"+thirdinput+"First Column :"+s3);
                System.out.println();
                System.out.println("U(3) is :"+s1+" "+s2+" "+s3);
                System.out.println("Sum of U(3) is :" +sum);
                
        }
        else 
            {
            System.out.println("P Value is Not Equal to 1 ");
            }
        
    }
    else
        System.out.println("Markov Chain Invalid Row not Equal to 1");   
    }
}

1 Ответ

0 голосов
/ 11 июля 2020

У вас проблема с этим кодом:

double m[][] = {{oneM, twoM, threeM, fourM, fiveM, sixM, sevenM, eightM, nineM}};

Это не матрица 3x3, а 1x9

Измените код на этот:

double m[][] = {{oneM, twoM, threeM}, {fourM, fiveM, sixM},{ sevenM, eightM, nineM}};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...