2016-04-30 75 views
1

給定一個NxNxN立方體(圖像),如何找到NxNxN立方體內的所有2x2x2方塊?當然如果N是偶數的話,我們可以找到2×2×2的盒子而沒有重疊,但是當N是奇數時,在更大的立方體中找到的一些2×2×2盒子之間有重疊。如何在MATLAB中查找更大的立方體中的小立方體?

所以,

1 - 我怎樣才能找到一個更大的NxNxN立方體,其中N是甚至所有非重疊2x2x2的盒子?

輸入:NxNxN立方體 輸出:所有可能的非重疊2x2x2的立方體。

2-如何在一個更大的NxNxN立方體中找到所有重疊的2x2x2盒子,其中N是奇數?這次,2x2x2框中的重疊區域在第二次(或更多次)訪問中應該爲零;即每個重疊區域應該被訪問(計數)一次。

輸入:NxNxN立方體 輸出:所有可能的重疊2x2x2的立方體與在2或更多的訪問重疊體素零個值。

+0

你是否總是做雙重職位? 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

回答

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也是如此。