2016-03-17 76 views
3

給定一個二維數組,我需要提出一個輸出質心的算法。我提出了下面的算法,但是,當數組大小增加到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 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; 

我知道它應該處理行和列的平均值。所以在我的算法中,我找到了行和列的方法,然後進行上面顯示的計算。相同的算法適用於列。

任何和所有的幫助表示讚賞。也許,我對質心的理解是不正確的。如果事情不清楚,那就問問。這是我自己的算法,是根據我對質心的理解而創建的,所以如果它不明確,請不要問。謝謝!

+0

看看這裏:https://en.wikipedia.org/wiki/Center_of_mass我想說如果你把細胞看作是在二維平面上等距離排列的粒子,你應該能夠調整相應的方程。 – Thomas

+0

你的算法沒有提到距離,這是關鍵(雙關意圖)。 – weston

+0

@Thomas,Cells表示二維數組中的每個單獨值?並使用在粒子系統下找到的方程式,並將m替換爲二維數組中的值,並且由於距離相等,R不包含在內?謝謝。 – user3453250

回答

2

擴大對我的評論,你應該能夠計算重心如下:

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 

的想法是基於部分https://en.wikipedia.org/wiki/Center_of_mass「粒子系統」:對待矩陣元素同樣在2D平面上佈置間隔的粒子。每個元素的位置等於它在矩陣內的位置,即列和行,而粒子質量是該單元/元素/矩陣位置的值。使用(現已刪除)測試用例

例-實現:

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]; 

上面的例子將返回座標2/2與是5x5矩陣的中心。

+1

儘管'centerOfMassValue'有問題。但是,對於其他所有內容+1 – Teepeemm

+0

@Teepeemm我剛剛添加了'centerOfMassValue',因爲他指的是問題原始版本中的值。這可能沒有多大用處,除此之外:) – Thomas

+0

優秀的解釋。非常感謝托馬斯。我似乎已經不正確地理解和實施了算法。 – user3453250

1

你需要做的加權平均,從而爲一個3×3陣列,

X =(質量(COL1)* 1 +質量(COL2)* 2 +質量(COL3)* 3)/(質量(COL1 )+質量(col2)+質量(col3))

並且類似地用y替換具有行的列。

一旦你有了這兩個值,它們的對就會告訴你陣列質心的x和y座標。

參見下面的鏈接示例之一,如果你需要一個視覺例如:http://www.batesville.k12.in.us/physics/APPhyNet/Dynamics/Center%20of%20Mass/2D_1.html

0

我假定自從你是存儲在一個矩陣的權重,即在矩陣中的位置將與重量的座標對應列索引是x行索引是y。因此,在行= 2,列= 3時,我們將在x/y座標系上取(3,2)。

此代碼如下維基百科上的solution for center of mass from a system of particles

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); 
} 

全部工作代碼here