Code covered by the BSD License  

Highlights from
reshape2cube

from reshape2cube by david s
Reshape a vector into a cubic array

reshape2cube(array, dimension)
function cube = reshape2cube(array, dimension)
% RESHAPE2CUBE   Reshape vector to a cube
%    C = RESHAPE2CUBE(A) returns an array of size [N,N,N] if A is an array
%    with N^3 elements.
%    C = RESHAPE2CUBE(A, D) generalises to output an N^D array.
%
%    Example:
%    A = 1:8;
%    C = reshape2cube(A)
% C(:,:,1) =
% 
%      1     3
%      2     4
% 
% 
% C(:,:,2) =
% 
%      5     7
%      6     8

% by David Szotten 2009

	if (nargin == 1)
		dimension = 3;
	end

	nElements = numel(array);
	
	cubeSide = round(nElements^(1/dimension));
	
	if( cubeSide^dimension ~= nElements )
		error('Number of elements do not match!');
	end
	
	cube = reshape(array, cubeSide * ones(1, dimension));
	
end

Contact us at files@mathworks.com