Java 矩阵质心
给定一个二维阵列,我需要想出一个算法来输出质心。我提出了下面的算法,但是,当数组大小增加到10 x 10矩阵时,它产生了错误的解。我使用java编写并运行了算法。我没有在这里提供代码,只是解释了我的算法,因为我觉得它不正确。然而,我无法找出原因Java 矩阵质心,java,algorithm,centroid,Java,Algorithm,Centroid,给定一个二维阵列,我需要想出一个算法来输出质心。我提出了下面的算法,但是,当数组大小增加到10 x 10矩阵时,它产生了错误的解。我使用java编写并运行了算法。我没有在这里提供代码,只是解释了我的算法,因为我觉得它不正确。然而,我无法找出原因 Store into an array: Mean of each row Store into an array: Mean of each column The algo below is used for row and column: Loop
Store into an array: Mean of each row
Store into an array: Mean of each column
The algo below is used for row and column:
Loop through the row array,
if(row = 1){
value = (mean of row 1) - (mean of row 2 + mean of row 3+ mean of row 4)
}else if(row =Length of array){
value = (mean of row 1 + mean of row 2 + mean of row 3) - (mean of row 4)}
else{
value = (mean of rows until ith row) - (ith row till end of array)
}
final value = lowest value;
我知道它应该处理行和列的平均值。因此,在我的算法中,我找出了行和列的平均值,然后进行上面所示的计算。相同的算法适用于列
非常感谢您的帮助。也许,我对质心的理解是错误的。如果有不清楚的地方,一定要问。这是我自己的算法,根据我对质心的理解创建的,所以如果不清楚,请询问。谢谢大家! 扩展我的评论,你应该能够计算重心,如下所示:
foreach col
foreach row
massvector.x += matrix[col][row] * col
massvector.y += matrix[col][row] * row
totalmass += matrix[col][row]
massvector.x /= totalmass
massvector.y /= totalmass
该想法基于中的“粒子系统”一节:将矩阵元素视为在二维平面上布置的等间距粒子。每个元素的位置等于其在矩阵中的位置,即列和行,而粒子质量是该单元/元素/矩阵位置的值
使用(现已删除)测试用例的示例实现:
double[][] matrix = new double[][]{
{0.70,0.75,0.70,0.75,0.80},
{0.55,0.30,0.20,0.10,0.70},
{0.80,0.10,0.00,0.00,0.80},
{0.70,0.00,0.00,0.00,0.80},
{0.80,0.90,0.80,0.75,0.90}};
double cx = 0;
double cy = 0;
double m = 0;
for(int x = 0; x < matrix.length; x++ ) {
for(int y = 0; y < matrix[x].length; y++) {
cx += matrix[x][y] * x;
cy += matrix[x][y] * y;
m += matrix[x][y];
}
}
//those are center's the cell coordinates within the matrix
int cmx = (int)(cx/m);
int cmy = (int)(cy/m);
//whatever you'd need that value for (the position is more likely what you're after)
double centerOfMassValue = matrix[cmx][cmy];
double[][]矩阵=新的double[][]{
{0.70,0.75,0.70,0.75,0.80},
{0.55,0.30,0.20,0.10,0.70},
{0.80,0.10,0.00,0.00,0.80},
{0.70,0.00,0.00,0.00,0.80},
{0.80,0.90,0.80,0.75,0.90}};
双cx=0;
双cy=0;
双m=0;
对于(int x=0;x
上面的示例将返回坐标2/2,它是5x5矩阵的中心。您需要对3x3数组进行加权平均 x̄=(质量(col1)*1+质量(col2)*2+质量(col3)*3)/(质量(col1)+质量(col2)+质量(col3)) 类似地,y将列替换为行 获得这两个值后,这对值将告诉您阵列质心的x和y坐标
如果您需要一个可视示例,请参见以下链接中的示例一:我假设,由于您将权重存储在矩阵中,因此矩阵中的位置将与权重坐标相对应,其中列索引为x,行索引为y。因此,在x/y坐标系中,行=2,列=3处的权重为(3,2) 此代码遵循Wikipedia上的:
public static Point2D.Double getCenterOfMass( double[][] matrix) {
double massTotal = 0;
double xTotal = 0;
double yTotal = 0;
for (int rowIndex = 0; rowIndex < matrix.length; rowIndex++) {
for (int colIndex = 0; colIndex < matrix[0].length; colIndex++) {
massTotal += matrix[rowIndex][colIndex];
xTotal += matrix[rowIndex][colIndex] * colIndex;
yTotal += matrix[rowIndex][colIndex] * rowIndex;
}
}
xTotal /= massTotal;
yTotal /= massTotal;
return new Point2D.Double(xTotal,yTotal);
}
publicstaticpoint2d.Double getCenterOfMass(Double[][]矩阵){
双倍总质量=0;
双外推=0;
双Y总=0;
对于(int-rowIndex=0;rowIndex
完整的工作代码。看看这里:我想说,如果你将细胞视为二维平面上等距排列的粒子,你应该能够适应相应的方程。你的算法没有提到距离,这是关键(双关语)。@Thomas,细胞指的是二维数组中的每个值?使用粒子系统下的方程,用2d数组中的值替换m,R不包括在内,因为距离相等?谢谢。是的单元格表示单个值,位置将由列和行索引定义。@weston Hmmm..我查看了给出的公式,但不确定要添加什么值作为距离,因为这是一个2D数组,值范围为0-1。尽管
centerOfMassValue
的使用有问题。但是其他的都是+1。@Teepeemm我刚刚添加了centerOfMassValue
,因为他指的是问题原始版本中的值。除此之外,它可能没有多大用处:)很好的解释。非常感谢你,托马斯。我似乎不正确地理解并实现了算法。