数组对角邻域分析与排序

发布于 2024-12-12 06:57:18 字数 2424 浏览 1 评论 0原文

我已经为此奋斗了一段时间,但似乎毫无进展。设置是这样的;我有一个二维数组。对于这个数组,我需要迭代每个值并返回对角邻居(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-12-19 06:57:19

我看到的主要问题是,每次遍历您的内部循环时:

for (int i = 1; i < data.length; i++){             
   for (int j = 1; j < data[i].length; j++) {

您调用的位置:

double [] tempArray = getDiagonals(i, j);

您正在将 datap所有值重置为当前计算的值中位数。要解决此问题,您需要某种方法来仅指示要填充的特定 datap 值的索引。

您需要替换这部分代码:

for (int z = 0; z < datap.length; z++){
    for (int y = 0; y < datap[z].length; y++){
    datap[z][y] = median;
    }
}   

您可以在方法顶部声明 int y, z 并执行如下操作:

if (y < datap.length){
    if (z == datap.length[y] - 1){
        y++;
        z = 0;
    }

    datap[y][z] = median;
    z++;
}

这样您就只分配给 中的特定索引datap 您试图访问的数据,而不是重置它的每个值。

The main issue that I see, is that with each traversal through your inner loop of:

for (int i = 1; i < data.length; i++){             
   for (int j = 1; j < data[i].length; j++) {

Where you call:

double [] tempArray = getDiagonals(i, j);

You are resetting all of the values of datap to be the current calculated median. To fix, you would need some way to indicate only the indices of the particular datap value that you want to populate.

You need to replace this section of your code:

for (int z = 0; z < datap.length; z++){
    for (int y = 0; y < datap[z].length; y++){
    datap[z][y] = median;
    }
}   

You could declare int y, z at the top of the method and do something like this:

if (y < datap.length){
    if (z == datap.length[y] - 1){
        y++;
        z = 0;
    }

    datap[y][z] = median;
    z++;
}

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.

飘然心甜 2024-12-19 06:57:19

终于破解了。要填充整个数组,以下代码可以正常工作。

//Diagonal to 1dArray and sorting

double [] [] bubbles()
{
    double [][] tempArray = new double [300][300];

    int y = 0;
    int z = 0;
    double median = 0;

    for (int i = 0; i < data.length; i++)
    {
        for (int j = 0; j < data[i].length; j++)
        {
            if ((i > 0) && (j > 0))
            {
                if ((i +1 < data[i].length) && (j +1 < data[j].length))
                {
                    double [] diagonals = getDiagonals(i, j);

                    //Need to sort here
                    median = diagonals[2];
                    tempArray[i][j] = median;
                }
            }
        }
    }
    return tempArray;
}

排序已被取出,我还没有将其放回去进行测试;但到目前为止,这为临时数组中的所有单元格提供了新值。

Finally cracked it. To populate the whole array the following code works a peach.

//Diagonal to 1dArray and sorting

double [] [] bubbles()
{
    double [][] tempArray = new double [300][300];

    int y = 0;
    int z = 0;
    double median = 0;

    for (int i = 0; i < data.length; i++)
    {
        for (int j = 0; j < data[i].length; j++)
        {
            if ((i > 0) && (j > 0))
            {
                if ((i +1 < data[i].length) && (j +1 < data[j].length))
                {
                    double [] diagonals = getDiagonals(i, j);

                    //Need to sort here
                    median = diagonals[2];
                    tempArray[i][j] = median;
                }
            }
        }
    }
    return tempArray;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文