数组对角邻域分析与排序
我已经为此奋斗了一段时间,但似乎毫无进展。设置是这样的;我有一个二维数组。对于这个数组,我需要迭代每个值并返回对角邻居(5 个值)。这些邻居将被放入一个新的一维 [5] 数组中并进行冒泡排序。然后,中间值(中位数)将被返回并放入新的中位数数组中。
到目前为止,我有提取对角邻居的方法:
//get diagonals from original DEM
double [] getDiagonals(int i, int j) {
double [] tempArray = new double [5];
tempArray[0] = data[i -1][j +1];
tempArray[1] = data[i -1][j -1];
tempArray[2] = data[i][j];
tempArray[3] = data[i +1][j -1];
tempArray[4] = data[i +1][j +1];
return tempArray;
}
然后我在迭代中使用此方法来获取原始数组中每个值的对角线:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
//do something with the tempArray
我认为这就是我要脱节的地方。通过测试 getDiagonals 方法工作正常。我正在努力从 bubbles() 方法中获取 tempArray 。如果我将输出设置为 tempArray,它只会返回为原始数组右下角计算的 5 个值。
我尝试将其他方法调用到 bubbles() 方法中,以便在那里完成所有处理并返回一个新数组:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
double sorted [] = sort(tempArray);
double median = sorted[2];
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
}
}
}
}
return datap;
}
这再次失败,输出 datap 只是零。上面的 sort() 方法将对角线传递给冒泡排序方法(我知道它可以工作)
我想我的问题是如何在迭代和填充新数组的方法中进行处理?
我希望这是有道理的,但如果你需要更多细节,请告诉我。是的,我使用的是冒泡排序,但这是我正在做的课程,所以必须使用它。对于java来说非常陌生,
我们将不胜感激 。 (如果我需要使用您提供的一些代码,我什至会参考您;)
I've been battling with this for some time and seem to be getting nowhere. The set up is so; I have a 2D array. For this array I need to iterate through each value and return the diagonal neighbours (5 values). These neighbours will be put into a new 1D [5] array and bubblesorted. The middle value (median) will then be returned and put into a new array of medians.
So far I have methods for extracting the diagonal neighbours:
//get diagonals from original DEM
double [] getDiagonals(int i, int j) {
double [] tempArray = new double [5];
tempArray[0] = data[i -1][j +1];
tempArray[1] = data[i -1][j -1];
tempArray[2] = data[i][j];
tempArray[3] = data[i +1][j -1];
tempArray[4] = data[i +1][j +1];
return tempArray;
}
I've then used this method in an iteration to get diagonals for each value in the original array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
//do something with the tempArray
I think this is where I'm coming unstuck. Through testing the getDiagonals method works fine. I'm struggling to get the tempArray out of the bubbles() method. If I set the output to be the tempArray it only returns the 5 values calculated for the bottom right corner of the original array.
I've tried calling other methods into the bubbles() method in order to do all the processing there and return a new array:
//get diagonals for each
double [] [] bubbles(){
double [] [] datap = new double [298] [298];
for (int i = 1; i < data.length; i++){
for (int j = 1; j < data[i].length; j++) {
if ((i > 0) && (j > 0)) {
if ((i < data.length-1) && (j < data.length-1)){
double [] tempArray = getDiagonals(i, j);
double sorted [] = sort(tempArray);
double median = sorted[2];
for (int z = 0; z < datap.length; z++){
for (int y = 0; y < datap[z].length; y++){
datap[z][y] = median;
}
}
}
}
}
}
return datap;
}
Again this fails and the output datap is just zeros. The sort() method above passed out the diagonals to a bubble sort method (which I know works on its
I guess my question is how to process within a method that is iterating and populate a new array?
I hope this makes sense but if you need more details please let me know. And yes, the sort I'm using is a bubble sort. I know they are rubbish but this is for a course I'm doing so it has to be used. And yes, I'm pretty new to java.
Any help would be greatly appreciated (and I'll even reference you if I need to use some code you provide ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到的主要问题是,每次遍历您的内部循环时:
您调用的位置:
您正在将
datap
的所有值重置为当前计算的值中位数
。要解决此问题,您需要某种方法来仅指示要填充的特定datap
值的索引。您需要替换这部分代码:
您可以在方法顶部声明
int y, z
并执行如下操作:这样您就只分配给
中的特定索引datap
您试图访问的数据,而不是重置它的每个值。The main issue that I see, is that with each traversal through your inner loop of:
Where you call:
You are resetting all of the values of
datap
to be the current calculatedmedian
. To fix, you would need some way to indicate only the indices of the particulardatap
value that you want to populate.You need to replace this section of your code:
You could declare
int y, z
at the top of the method and do something like this:That way you are only assigning to the specific index in
datap
that you are trying to reach, instead of resetting each of its values.终于破解了。要填充整个数组,以下代码可以正常工作。
排序已被取出,我还没有将其放回去进行测试;但到目前为止,这为临时数组中的所有单元格提供了新值。
Finally cracked it. To populate the whole array the following code works a peach.
The sorting was taken out and I haven't tested with it back in yet; but so far this is providing new values for all the cells in the temp array.