我会给你一个答案,即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也是如此。
来源
2016-04-30 08:06:30
tim
你是否总是做双重职位? https://de.mathworks.com/matlabcentral/answers/281798-how-to-find-all-possible-small-boxes-e-g-2x2x2-and-their-overlaps-within-a-bigger-nxnxn-cube – tim