Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save tonyfast/ec4e0dc3cf39869f430d to your computer and use it in GitHub Desktop.

Select an option

Save tonyfast/ec4e0dc3cf39869f430d to your computer and use it in GitHub Desktop.
Partition Portions of N-D Arrays in Matlab using either a center of a region or corner of a region as a reference.
function part = partition_space( Data,varargin );
% Remove subsets of information from large volumetric images
% Two options
% # ``corner`` <<default>> - Reference position is the top left corner of
% the image.
% ``window`` - is a vector where each element defines the window
% size in a given direction
% # ``center`` - the center of the iamge
% ``window`` - is a column vector where each row is the dimension
% of the array and the first a distance less than the center and
% the second column is a right greater than the center in its
% respecitve dimension.
% 'window, [-10 10;
% -2 31]
% A window 10 left to 10 right of center(1) &
% A window -2 left to 31 right of center(2)
OSZ = size( Data );
DIM = ndims( Data );
param = setparam( OSZ, DIM, varargin{:} );
switch param.type
case 'center'
param.args.center(numel(param.args.center)+1:3) = 1;
if isvector( param.args.center )
lastid = 1;
else
lastid = size( param.args.center,1);
end
for ii = 1 : lastid
tempargs = struct( 'center', param.args.center(ii,:), ...
'window', param.args.window );
v{ii} = cntr_args2vec( tempargs, OSZ );
end
case 'box'
% For linspace
param.args.corner(numel(param.args.corner)+1:3) = 1;
if isvector( param.args.corner )
lastid = 1;
else
lastid = size( param.args.corner,1);
end
for ii = 1 : lastid
tempargs = struct( 'corner', param.args.corner(ii,:), ...
'window', param.args.window );
v{ii} = box_args2vec( tempargs, OSZ );
end
end
part = cellfun( @(v) Data( v{1}, v{2}, v{3} ), v , 'UniformOutput',false );
if numel( part ) == 1
part = part{1};
end
end % partition_space
%%
function vec = box_args2vec( args, OSZ )
% Express the window limits as vector
for ii = 1 : 3
vec{ii} = [1 : args.window(ii) ] + args.corner(ii) - 1;
vec{ii}( vec{ii} < 1 | vec{ii} > OSZ(ii) ) = [];
end % for ii
end % box_args2vec
function vec = cntr_args2vec( args, OSZ )
% Express the window limits as vector
for ii = 1 : 3
vec{ii} = [args.window(ii,1) : args.window(ii,2) ] + args.center(ii);
vec{ii}( vec{ii} < 1 | vec{ii} > OSZ(ii) ) = [];
end % for ii
end % cntr_args2vec
function param = setparam( OSZ, DIM, varargin )
% Set function parameters
param = struct( 'type', 'box' ,...
'args', struct( 'corner', [1 1 1], ...
'window',[10 10 10] ) );
for ii = 1 : 2 : numel(varargin)
switch varargin{ii}
case 'corner'
param = struct( 'type', 'box' ,...
'args', struct( 'corner', [1 1 1], ...
'window',[10 10 10] ) );
case 'center'
param = struct( 'type', 'center' ,...
'args', struct( 'center', [1 1 1], ...
'window',[-10 10 ; -10 10; -10 10] ) );
end % switch varargin
end % for ii
for ii = 1 : 2 : numel(varargin)
if ismember( varargin{ii} , {'corner', 'window', 'center' } )
param.args.(varargin{ii} ) = varargin{ii+1};
end % switch varargin
end % for ii
end % function setparam
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment