从2D数组创建距离矩阵

时间:2022-07-08 21:31:31

I have a 2D Array (inputMatrix with x rows and y columns) and need to subtract all vectors(rows) with eachother.

我有一个2D数组(inputMatrix有x行和y列),需要相互减去所有向量(行)。

Here's the input:

1,5
3,7
2,3
6,5
4,7

The output(a distance matrix) should look like:

输出(距离矩阵)应如下所示:

subtraction[0][0] = {0,0} // first row - first row
subtraction[0][1] = {-2,-2} // first row - 2nd row -> {1,5}-{3,7}=-2,2
subtraction[0][2] = {-1,2} // first row - 3rd row 
...
subtraction[4][2] = {2,4}
subtraction[4][3] = {-2,2}
subtraction[4][4] = {0,0}

However i'm having a problem on storing the values, since subtraction[row][col] values are being overwritten on the "col for-loop". Also a note, each subtraction index is getting as output another array.

但是我在存储值时遇到问题,因为减法[row] [col]值正在“col for-loop”上被覆盖。另外注意,每个减法索引都是另一个数组的输出。

for(int row = 0; row < inputMatrix.length; row++){
            for(int col = 0; col < inputMatrix[0].length; col++){
                subtraction[row][col] = inputMatrix[0][row] - inputMatrix[row][col];
                System.out.print(subtraction[row][col] + " ");
            }
        System.out.print("\n");
    }

2 个解决方案

#1


1  

Based on your description of the output, you need a 3-dimensional array to store the output, since for each pair of input rows, you are producing an output row.

根据您对输出的描述,您需要一个三维数组来存储输出,因为对于每对输入行,您将生成一个输出行。

for(int row1 = 0; row1 < inputMatrix.length; row1++){
   for(int row2 = 0; row2 < inputMatrix.length; row2++){
        for(int col = 0; col < inputMatrix[0].length; col++){
            subtraction[row1][row2][col] = inputMatrix[row1][col] - inputMatrix[row2][col];
        }
    }
}

If you must have a two dimensional output, you can flatten the output :

如果必须具有二维输出,则可以展平输出:

int outputRow = 0;
for(int row1 = 0; row1 < inputMatrix.length; row1++){
   for(int row2 = 0; row2 < inputMatrix.length; row2++){
        for(int col = 0; col < inputMatrix[0].length; col++){
            subtraction[outputRow][col] = inputMatrix[row1][col] - inputMatrix[row2][col];
        }
        outputRow++;
    }
}

#2


0  

subtraction[row][col] = inputMatrix[0][row] - inputMatrix[**row**][**col**];

减法[row] [col] = inputMatrix [0] [row] - inputMatrix [** row **] [** col **];

You've mixed up your indices here.

你在这里混淆了你的指数。

#1


1  

Based on your description of the output, you need a 3-dimensional array to store the output, since for each pair of input rows, you are producing an output row.

根据您对输出的描述,您需要一个三维数组来存储输出,因为对于每对输入行,您将生成一个输出行。

for(int row1 = 0; row1 < inputMatrix.length; row1++){
   for(int row2 = 0; row2 < inputMatrix.length; row2++){
        for(int col = 0; col < inputMatrix[0].length; col++){
            subtraction[row1][row2][col] = inputMatrix[row1][col] - inputMatrix[row2][col];
        }
    }
}

If you must have a two dimensional output, you can flatten the output :

如果必须具有二维输出,则可以展平输出:

int outputRow = 0;
for(int row1 = 0; row1 < inputMatrix.length; row1++){
   for(int row2 = 0; row2 < inputMatrix.length; row2++){
        for(int col = 0; col < inputMatrix[0].length; col++){
            subtraction[outputRow][col] = inputMatrix[row1][col] - inputMatrix[row2][col];
        }
        outputRow++;
    }
}

#2


0  

subtraction[row][col] = inputMatrix[0][row] - inputMatrix[**row**][**col**];

减法[row] [col] = inputMatrix [0] [row] - inputMatrix [** row **] [** col **];

You've mixed up your indices here.

你在这里混淆了你的指数。