在按列排序的二维数组中查找元素的索引。

huangapple go评论62阅读模式
英文:

Finding indexes of 2d array elements in a sorted 2d array by columns

问题

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j < Sjm1.length; j++) {
        for (int m = 0; m < Sjm1[0].length; m++) {
            int targetValue = Sorted_Sjm2[j][m];
            for (int i = 0; i < Sjm1.length; i++) {
                if (Sjm1[i][m] == targetValue) {
                    output[j][m] = i;
                    break;
                }
            }
        }
    }
    return output;
}
英文:

I have two 2D arrays of integers of the same dimension that I need to compare. I am going to explain what I need with an example where Sjm1 is the "original" array and the Sjm2 array has the same values as Sjm1 but the values of each column are ordered in increasing order (i.e. "0" is the min value of Sjm1 in column 0 so Sjm2[0][0]=0; then "70" is the next min value of Sjm1 in column 0 ⇒ Sjm2[1][0]=70; and so on). I have a method to sort a "Sjm1" array to "Sjm2".

在按列排序的二维数组中查找元素的索引。

在按列排序的二维数组中查找元素的索引。

Now I need to build an "output" array (2D array of integers) where the values in each column represent the number of the row in Sjm1 array that coincides with the elements of the column in Sjm2. For example, Output[0][0]=5 because Sjm1[0][0]=366 is Sjm2[5][0]=366; Output[1][0]=2 because Sjm1[1][0]=104 is Sjm2[2][0]=104; and so on).

Thus, for the previously presented example, the needed output must be the following:

在按列排序的二维数组中查找元素的索引。

I tried to build a method in Java where I pass the two 2D arrays of integers but is not working as needed:

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j &lt; Sjm1.length; j++) {
        k = 0;
        for (int m = 0; m &lt; Sjm1[0].length; m++) {
            if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
                output[j][m] = k;
                k++;
            }
        }
    }
    return output;
}

The output is clearly not what I need:

[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]

I'll be glad if someone can help me.

答案1

得分: 1

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j < Sjm1.length; j++) {
        k = 0;
        for (int m = 0; m < Sjm1[0].length; m++) {
            if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
                k = j;
                output[j][m] = k;
                break;
            }
        }
    }
    return output;
}
英文:

I think you are not updating the value of k correctly, if I understood what you need, once you find the value you are looking for just update the value of k to the index you found the value in. Note that if you have repeated values it will only take the first it found.

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j &lt; Sjm1.length; j++) {
        k = 0;
        for (int m = 0; m &lt; Sjm1[0].length; m++) {
            if (Sorted_Sjm2[j][m] == Sjm1[j][m]) {
                k = j;
                output[j][m] = k;
                break;
            }
        }
    }
    return output;
}

答案2

得分: 0

如果我理解你的问题正确,问题出在变量 k 上。

它代表了第二个数组上的行索引,在比较两个数组上的值时,你应该使用 k 来索引第二个数组中的行。

此外,你应该使 k 在当前列上迭代所有行,所以代码应该如下所示(我修改了变量以使其更加描述性):

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int[][] output = new int[Sjm1.length][Sjm1[0].length];

    for (int row = 0; row < Sjm1.length; row++) {
        for (int column = 0; column < Sjm1[0].length; column++) {
            int valueToSearchFor = Sjm1[row][column];

            for (int rowInSorted = 0; rowInSorted < Sorted_Sjm2.length; rowInSorted++) {
                if (Sorted_Sjm2[rowInSorted][column] == valueToSearchFor) {
                    // 找到
                    output[row][column] = rowInSorted;
                    break;
                }
                // 未找到
                output[row][column] = -1;
            }
        }
    }

    return output;
}

请注意,尽管这段代码能够工作,但我怀疑它不是最优的,所以对于非常大的数据集,我不会使用它。

英文:

If I get your question right, the problem is with the k variable.

It represents the row index on the second array, so when you compare the value on the two array, you should index the row of the second array with k.

Also, you should iterate the k over all rows in the current column, so the code would look like this (I modified the variables to make them more describing):

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int[][] output = new int[Sjm1.length][Sjm1[0].length];

    for (int row = 0; row &lt; Sjm1.length; row++) {
        for (int column = 0; column &lt; Sjm1[0].length; column++) {
            int valueToSearchFor = Sjm1[row][column];

            for (int rowInSorted = 0; rowInSorted &lt; Sorted_Sjm2.length; rowInSorted++) {
                if (Sorted_Sjm2[rowInSorted][column] == valueToSearchFor) {
                    // Found
                    output[row][column] = rowInSorted;
                    break;
                }
                // Not found
                output[row][column] = -1;
            }
        }
    }

    return output;
}

Note that although this code works, I doubt that it's optimal, so I would not use it for very large data sets.

答案3

得分: 0

首先,您需要对Sorted_Sjm2[j][]进行迭代,以找出Sjm1[j][m]值所在的位置,因此您需要在for (int m = 0; m < Sjm1[0].length; m++)内部再使用另一个循环。另一件事是,为什么您在数组中使用k?k,却没有显示任何内容。如果您想获得排序后的位置,应该使用另一个变量,在Sorted_Sjm2[j][]的新的for语句中进行声明。因为我们知道您在第j列,所以我们只需要知道排序数组的行号。

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j < Sjm1.length; j++) {
        for (int m = 0; m < Sjm1[0].length; m++) {
            for (int d = 0; d < Sjm1[0].length; d++) {
                if (Sorted_Sjm2[j][d] == Sjm1[j][m]) {
                    output[j][m] = d;
                }
            }
        }
    }
    return output;
}
英文:

At first you need to iterate on Sorted_Sjm2[j][] to find out where is the Sjm1[j][m] value so you need another for in for (int m = 0; m &lt; Sjm1[0].length; m++). And the other thing is why you are using k?k doesn't show any thing in your array. If you want gain the sorted position you should use another variable that you declare in new for statement in
Sorted_Sjm2[j][]. Because we know you are in column j so we just need to now row number of the sorted array.

public static int[][] sec(int[][] Sjm1, int[][] Sorted_Sjm2) {
    int k;
    int[][] output = new int[Sjm1.length][Sjm1[0].length];
    for (int j = 0; j &lt; Sjm1.length; j++) {
        for (int m = 0; m &lt; Sjm1[0].length; m++) {
            for (int d = 0; m &lt; Sjm1[0].length; d++) {
                if (Sorted_Sjm2[j][d] == Sjm1[j][m]) {
                    output[j][m] = d;
                }
            }
        }
    }
    return output;
}

答案4

得分: 0

我对贡献还不太熟悉,所以如果我理解错了请告诉我!><"

问题出在你的代码上,你在相同的间隔(当 m 和 j 改变时)比较了两个矩阵。你可以这样做,遍历矩阵 sjm2,并在每次迭代时与矩阵 sjm1 进行比较。

例如:

  1. 获取 SJM1 的值。
  2. 遍历 SJM2 中的该列,查找具有相同值的行。
  3. 将行号添加到输出中。

另外,你之所以获得输出,唯一的可能性是你的 Sorted_Sjm2 与你的 Sjm1 相同,因为在每次迭代中 k 都增加到了 6。

英文:

I'm kinda new to contributing, so please let me know if I got this wrong! ><"

The problem is with your code, you're comparing both of the matrices at the same intervals (as m and j changes). What you could do, is iterate through matrix sjm2 and compare for each iteration for matrix sjm1.

i.e.

  1. Have value of SJM1
  2. Iterate through that column in SJM2 to find the row with the same val
  3. Add the row number in the output.

Plus, the only way you got your output is if your Sorted_Sjm2 is the same as your Sjm1, as k was incremented to 6 every iteration.

答案5

得分: 0

在你的内部循环中(用于设置 output[j][m] ),找到匹配索引的简单方法是使用 List.indexOf ,而不是自己搜索:

output[j][m] = Arrays.asList(Sjm2[j]).indexOf(Sjm1[j][m]);
英文:

Inside your inner loop (which sets output[j][m]) an easy way to find the matching index would be to use List.indexOf rather than searching for it yourself:

output[j][m] = Arrays.asList(Sjm2[j]).indexOf(Sjm1[j][m]);

答案6

得分: 0

获取按矩阵列排序的元素的行索引矩阵 - 可以首先对该矩阵的元素的行索引按列进行排序,然后获取索引的排序转置矩阵。然后将排序后的矩阵进行转置,并对每个元素交换其值和列索引:

int m = 7;
int n = 8;
int[][] arr1 = new int[][]{
        {366, 139, 223, 312, 563, 471, 437, 2},
        {104, 195, 85, 0, 377, 289, 227, 5},
        {451, 221, 329, 425, 523, 591, 537, 1},
        {208, 78, 0, 140, 437, 380, 286, 6},
        {0, 52, 114, 84, 296, 212, 205, 3},
        {70, 0, 40, 121, 194, 156, 123, 3},
        {299, 351, 446, 216, 648, 685, 571, 2}};
int[][] arr2 = IntStream
        // 遍历数组行的索引
        .range(0, n)
        .mapToObj(i -> IntStream
                // 遍历列的索引
                .range(0, m)
                .boxed()
                // 按数组中的值对列的元素的索引进行排序
                .sorted(Comparator.comparingInt(j -> arr1[j][i]))
                .mapToInt(Integer::intValue)
                // 排序后的索引列是新数组中的行
                .toArray())
        // 返回排序后的索引数组
        .toArray(int[][]::new);
// 转置索引数组
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i ->
        IntStream.range(0, n).forEach(j -> {
            // 交换列索引和元素的值
            int val = arr2[j][i];
            arr3[val][j] = i;
        }));
// 输出
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);

输出:

[5, 3, 4, 5, 5, 4, 4, 1]
[2, 4, 2, 0, 2, 2, 2, 5]
[6, 5, 5, 6, 4, 5, 5, 0]
[3, 2, 0, 3, 3, 3, 3, 6]
[0, 1, 3, 1, 1, 1, 1, 3]
[1, 0, 1, 2, 0, 0, 0, 4]
[4, 6, 6, 4, 6, 6, 6, 2]
英文:

To get a matrix of row indices by columns of elements of this matrix in a sorted matrix - you can first sort the row indices of the elements of this matrix by columns and get the sorted transposed matrix of indices. Then transpose the sorted matrix back and, for each element, swap its value and column index:

int m = 7;
int n = 8;
int[][] arr1 = new int[][]{
        {366, 139, 223, 312, 563, 471, 437, 2},
        {104, 195, 85, 0, 377, 289, 227, 5},
        {451, 221, 329, 425, 523, 591, 537, 1},
        {208, 78, 0, 140, 437, 380, 286, 6},
        {0, 52, 114, 84, 296, 212, 205, 3},
        {70, 0, 40, 121, 194, 156, 123, 3},
        {299, 351, 446, 216, 648, 685, 571, 2}};
int[][] arr2 = IntStream
        // iterate over the indices
        // of the rows of the array
        .range(0, n)
        .mapToObj(i -&gt; IntStream
                // iterate over the
                // indices of the columns
                .range(0, m)
                .boxed()
                // sort indices of the elements of the
                // columns by its values in the array
                .sorted(Comparator.comparingInt(j -&gt; arr1[j][i]))
                .mapToInt(Integer::intValue)
                // sorted column of indices
                // is a row in the new array
                .toArray())
        // return sorted array of indices
        .toArray(int[][]::new);
// transpose the array of indices
int[][] arr3 = new int[m][n];
IntStream.range(0, m).forEach(i -&gt;
        IntStream.range(0, n).forEach(j -&gt; {
            // swap the column index and
            // the value of the element
            int val = arr2[j][i];
            arr3[val][j] = i;
        }));
// output
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);

Output:

[5, 3, 4, 5, 5, 4, 4, 1]
[2, 4, 2, 0, 2, 2, 2, 5]
[6, 5, 5, 6, 4, 5, 5, 0]
[3, 2, 0, 3, 3, 3, 3, 6]
[0, 1, 3, 1, 1, 1, 1, 3]
[1, 0, 1, 2, 0, 0, 0, 4]
[4, 6, 6, 4, 6, 6, 6, 2]

<sup>See also:
<br>• Sorting 2d array of integers by column
<br>• Finding the position of a row element in a 2d ordered array</sup>

huangapple
  • 本文由 发表于 2020年10月21日 11:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64456361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定