如何计算网格中的重心? [英] How to calculate center of gravity in grid?

查看:111
本文介绍了如何计算网格中的重心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出具有x * y个单元格的网格(或表格).每个单元格包含一个值.这些单元中的大多数单元的值为0,但是在此网格上某处的某个单元格可能具有较高的热点".这样,该单元的邻居的值也将>0.离热点越远,相应网格单元中的值就越低.

Given a grid (or table) with x*y cells. Each cell contains a value. Most of these cells have a value of 0, but there may be a "hot spot" somewhere on this grid with a cell that has a high value. The neighbours of this cell then also have a value > 0. As farer away from the hot spot as lower the value in the respective grid cell.

因此,这个热点可以看作是一座山的顶部,数值越小,我们离这座山的距离就越远.在一定距离处,该值再次降至0.

So this hot spot can be seen as the top of a hill, with decreasing values the farer we are away from this hill. At a certain distance the values drop to 0 again.

现在,我需要确定网格中代表网格重心的单元.在上面的简单示例中,此质心将只是具有最高值的一个像元.但是,它并不总是那么简单:

Now I need to determine the cell within the grid that represents the grid's center of gravity. In the simple example above this centroid would simply be the one cell with the highest value. However it's not always that simple:

  1. 热点小区周围相邻小区的递减值可能分布不均,或者山坡"可能比另一边下降到0.

  1. the decreasing values of neighbour cells around the hot spot cell may not be equally distributed, or a "side of the hill" may fall down to 0 sooner than another side.

在网格中还有另一个热点/山峰,其值> 0,否则位于网格中.

there is another hot spot/hill with values > 0 elsewehere within the grid.

我可以认为这是一个典型的问题.不幸的是,我不是数学专家,所以我不知道要搜索什么(至少我没有在Google中找到答案).

I could think that this is kind of a typical problem. Unfortunately I am no math expert so I don't know what to search for (at least I have not found an answer in Google).

有什么主意我该如何解决这个问题?

Any ideas how can I solve this problem?

谢谢.

推荐答案

您正在寻找单元格值的加权平均值".假设每个单元格都有一个值z(x,y),那么您可以执行以下操作

You are looking for the "weighted mean" of the cell values. Assuming each cell has a value z(x,y), then you can do the following

zx = sum( z(x, y) ) over all values of y
zy = sum( z(x, y) ) over all values of x

meanX = sum( x * zx(x)) / sum ( zx(x) )
meanY = sum( y * zy(y)) / sum ( zy(y) )

我相信您可以将其转换为您选择的语言...

I trust you can convert this into a language of your choice...

示例:如果您了解Matlab,则上面的内容将编写如下

Example: if you know Matlab, then the above would be written as follows

zx = sum( Z, 1 ); % sum all the rows
zy = sum( Z, 2 ); % sum all the columns

[ny nx] = size(Z); % find out the dimensions of Z

meanX = sum((1:nx).*zx) / sum(zx);
meanY = sum((1:ny).*zy) / sum(zy);

这将为您提供在1 .. nx范围内的meanX:如果位于中间,则该值为(nx + 1)/2.您显然可以根据自己的需要进行缩放.

This would give you the meanX in the range 1 .. nx : if it's right in the middle, the value would be (nx+1)/2. You can obviously scale this to your needs.

用几乎真实"的代码再编辑一次:

one more time, in "almost real" code:

// array Z(N, M) contains values on an evenly spaced grid
// assume base 1 arrays

zx = zeros(N);
zy = zeros(M);

// create X profile:
for jj = 1 to M
  for ii = 1 to N
    zx(jj) = zx(jj) + Z(ii, jj);
  next ii
next jj

// create Y profile:
for ii = 1 to N
  for jj = 1 to M
    zy(ii) = zy(ii) + Z(ii, jj);
  next jj
next ii

xsum = 0;
zxsum = 0;
for ii = 1 to N
  zxsum += zx(ii);
  xsum += ii * zx(ii);
next ii
xmean = xsum / zxsum;

ysum = 0;
zysum = 0;
for jj = 1 to M
  zysum += zy(jj);
  ysum += jj * zy(ii);
next jj
ymean = ysum / zysum;

这篇关于如何计算网格中的重心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆