【问题标题】:How to find small cubes within a bigger cube in MATLAB?如何在 MATLAB 中找到较大立方体中的小立方体?
【发布时间】:2016-08-25 07:39:19
【问题描述】:

给定一个 NxNxN 立方体(图片),我怎样才能找到 NxNxN 立方体中的所有 2x2x2 框?当然如果 N 是偶数,我们可以找到 2x2x2 的盒子而不重叠,但是当 N 是奇数时,在更大的立方体中找到的一些 2x2x2 盒子之间存在重叠。

所以,

1- 如何在 N 为 偶数 的较大 NxNxN 立方体中找到所有不重叠的 2x2x2 框?

输入:NxNxN 立方体 输出:所有可能的非重叠 2x2x2 立方体。

2- 如何在 N 为 奇数 的较大 NxNxN 立方体中找到所有重叠的 2x2x2 框?这一次,2x2x2 框中的重叠区域在第二次(或更多)访问中应该为零;即每个重叠区域都应该被访问(计数)一次。

输入:NxNxN 立方体 输出:所有可能重叠的 2x2x2 立方体,在第 2 次或更多次访问中重叠体素的值为零。

【问题讨论】:

标签: image matlab 3d


【解决方案1】:

我会给你一个答案 N 是偶数的部分。其余的可以很容易地适应,我希望你可以自己做:-) 或者至少尝试一下 - 如果你有问题,请回到我们这里。

我没有再安装 MATLAB,所以我希望这里没有拼写错误。但是思路应该很清楚:

% 
N = 10;

% First create all possible starting coordinates of 2x2x2 cubes within the big cube:
coords = 1:2:(N-1); % could easily be adapted to other small-cube sizes like 3x3x3 if you want to...

% Now create all possible combinations of starting-coordinates in every direction (as it is a cube, the starting points in x, y, z directions are the same):
sets = {coords, coords, coords};
[x y z] = ndgrid(sets{:});
cartProd = [x(:) y(:) z(:)]; % taken from here: http://stackoverflow.com/a/4169488/701049 --> you could instead also use this function: https://www.mathworks.com/matlabcentral/fileexchange/10064-allcomb-varargin- which generates all possible combinations

% Now cartProd contains all possible start-points of small cubes as row-vectors. If you want, you can easily calculate the corresponding vectors of end-points by simply adding +1 to every entry which will effectively yield a small-cube size of 2. If you want to further modify it to support e.g. 3x3x3 cubes, simply add +2 to every dimension.
endPoints = cartProd+1;

% E.g.: the first small cube starts at [x,y,z] = cartProd(1,:) and ends at [x_e, y_e, z_e] = endPoints(1,:).

玩得开心:-)

提示:对于奇数大立方体 -> 只需将其视为大小均匀的立方体,例如将 9x9x9 立方体视为 10x10x10,从上面采用我的算法,然后将最外面的小立方体移到中心一步。这意味着,取具有最大 x、y 或 z 坐标的小立方体,并在该方向上减去 1。这样所有 x_max=9 的小立方体的起始坐标都将更改为 x=8。那么对于 y_max=9 和 z_max=9 也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多