我有一个数组来存储绘制一条线的一组坐标。所以下面是一些示例坐标使用markov链时获取ArrayIndexOutOfBoundsException
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。任何想法如何解决这个问题?
你能告诉你'markovMatrix'和'plotMatrix'的声明吗?当你逐步调试调试器中的代码时,矩阵中有多大,你期望它有多大? – 2011-05-30 15:09:13