立方体上的等距点 [英] Equidistant points across a cube

查看:35
本文介绍了立方体上的等距点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要初始化一些三维点,并且我希望它们在整个立方体中等距分布.有什么创造性的方法可以做到这一点吗?

I need to initialize some three dimensional points, and I want them to be equally spaced throughout a cube. Are there any creative ways to do this?

我正在使用迭代期望最大化算法,并且我希望我的初始向量能够均匀地跨越"空间.

I am using an iterative Expectation Maximization algorithm and I want my initial vectors to "span" the space evenly.

例如,假设我想在大小为 1x1x1 的立方体中均匀分布八个点.我想要边长为 0.333 的立方体的角上的点,以较大的立方体为中心.

For example, suppose I have eight points that I want to space equally in a cube sized 1x1x1. I would want the points at the corners of a cube with a side length of 0.333, centered within the larger cube.

下面是一个 2D 示例.请注意,红点彼此和边缘等距.我想要同样的 3D.

A 2D example is below. Notice that the red points are equidistant from eachother and the edges. I want the same for 3D.

如果点数没有整数立方根,我可以在排列中留下一些间隙".

In cases where the number of points does not have an integer cube root, I am fine with leaving some "gaps" in the arrangement.

目前我正在取点数的立方根并使用它来计算点数和它们之间的所需距离.然后我遍历这些点并增加 X、Y 和 Z 坐标(交错以便 Y 在 X 循环回 0 之前不会增加,对于 Y,Z 也是如此).

Currently I am taking the cube root of the number of points and using that to calculate the number of points and the desired distance between them. Then I iterate through the points and increment the X, Y and Z coordinates (staggered so that Y doesn't increment until X loops back to 0, same for Z with regard for Y).

如果在 MATLAB 中有一种简单的方法可以做到这一点,我很乐意使用它.

If there's an easy way to do this in MATLAB, I'd gladly use it.

推荐答案

对于点数不是完美立方体的情况,您必须更详细地定义问题.但是,对于点数是立方体的情况,您可以使用:

You'll have to define the problem in more detail for the cases where the number of points isn't a perfect cube. Hovever, for the cases where the number of points is a cube, you can use:

l=linspace(0,1,n+2);
x=l(2:n+1); y=x; z=x;
[X, Y, Z] = meshgrid(x, y, z);

然后对于矩阵中的每个位置,该点的坐标由 X、Y 和 Z 的相应元素给出.如果您希望将点列在单个矩阵中,这样每一行代表一个点,用x、y 和 z 坐标的三列,那么你可以说:

Then for each position in the matrices, the coordinates of that point are given by the corresponding elements of X, Y, and Z. If you want the points listed in a single matrix, such that each row represents a point, with the three columns for x, y, and z coordinates, then you can say:

points(:,1) = reshape(X, [], 1);
points(:,2) = reshape(Y, [], 1);
points(:,3) = reshape(Z, [], 1);

您现在在整个单位立方体的网格上有一个 n^3 点的列表,不包括边界.正如其他人所建议的那样,如果您想要更少的点,您可以随机删除一些点.这很容易做到,通过使用 randi([0 n^3], a, 1) 生成要删除的点的 a 索引.(不要忘记检查 randi() 返回的矩阵中的重复项,否则您可能不会删除足够的点.)

You now have a list of n^3 points on a grid throughout the unit cube, excluding the boundaries. As others have suggested, you can probably randomly remove some of the points if you want fewer points. This would be easy to do, by using randi([0 n^3], a, 1) to generate a indices of points to remove. (Don't forget to check for duplicates in the matrix returned by randi(), otherwise you might not delete enough points.)

这篇关于立方体上的等距点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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