0

我有一个数组来存储一组用于绘制一条线的坐标。所以这里有一些示例坐标

double[][] plotMatrix = {{10,20},{55,80},
                         {120,40},{225,30},
                         {327.5,100},
                         {427.5,30},
                         {529,60}};

下一步是创建一个二维的马尔可夫矩阵。

在此处输入图像描述

首先,我计算左列中的点后跟上列中的点的时间。因为我想要一条线,所以每个点后面跟着另一个单点。这意味着如果我们有 {10,20} 作为输入,{55,80} 成为下一个点的概率是 100%。

我不太确定这一切,所以请纠正我!

所以这是我的矩阵

double[][] markovMatrix = { {0.0,1.0,0.0,0.0,0.0,0.0,0.0},
                                    {0.0,0.0,1.0,0.0,0.0,0.0,0.0},
                                    {0.0,0.0,0.0,1.0,0.0,0.0,0.0},
                                    {0.0,0.0,0.0,0.0,1.0,0.0,0.0},
                                    {0.0,0.0,0.0,0.0,0.0,1.0,0.0},
                                    {0.0,0.0,0.0,0.0,0.0,0.0,1.0},
                                    {0.0,0.0,0.0,0.0,0.0,0.0,0.0}};

我的算法:

    int seed = 0;
    int output = 0;

    for(int i = 0; i < 40;i++){
        double choice = r.nextDouble();

        double currentSum = 0.0;

        for(;output < markovMatrix.length;output++){

            currentSum += markovMatrix[seed][output];

            if(choice <= currentSum){
                break;
            }
        }

        System.out.println(output);
        polygon.lineTo(plotMatrix[output][0], plotMatrix[output][1]);

        seed = output;

        output = 0;
    }

我的问题是,ArrayOutOfBoundsException:7当我尝试访问 plotMatrix 和 markovMatrix 时,我得到了一个。但是,在每个循环结束时输出设置为 0。任何想法如何解决这个问题?

4

2 回答 2

3

我不太确定它是否正确答案,

但是 for(;output < markovMatrix.length;output++) 将从 0 步进到 7,而 markovMatrix 中只有 0 到 6 个条目。

使用 for(;output < markovMatrix.length-1;output++) 通过从 1 步进到 6 来修复 ArrayIndexOutOfBoundsException。

但是我怀疑你真的想从 0 到 6。这就是你的问题。

于 2011-05-30T15:07:57.417 回答
1

何时使用内部循环 output=7 完成循环,这是数组的长度。您应该跳过最后一次迭代,因为您的数组索引是从 0 到 6。

于 2011-05-30T15:08:53.087 回答