Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slpartition
Home > sltoolbox > utils > slpartition.m

slpartition

PURPOSE ^

SLPARTITION Partition a range into blocks in a specified manner

SYNOPSIS ^

function PS = slpartition(whole_size, spec_item, varargin)

DESCRIPTION ^

SLPARTITION Partition a range into blocks in a specified manner

 $ Syntax $
   - PS = slpartition(whole_size, 'numblks', nblks_dim1, nblks_dim2, ...);
   - PS = slpartition(whole_size, 'numblks', [nblks_dim1, nblks_dim2, ...]);
   - PS = slpartition(whole_size, 'maxblksize', mbs_dim1, mbs_dim2, ...);
   - PS = slpartition(whole_size, 'maxblksize', [mbs_dim1, mbs_dim2, ...]);
   - PS = slpartition(whole_size, 'blksizes', blksizes_dim1, blksizes_dim2, ...);
   - PS = slpartition(whole_size, 'startinds', startinds_dim1, startinds_dim2, ...);
   - PS = slpartition(whole_size, 'endinds', endinds_dim1, endinds_dim2, ...);

 $ Description $
   
   - PS = slpartition(whole_size, 'numblks', nblks_dim1, nblks_dim2, ...) 
     PS = slpartition(whole_size, 'numblks', [nblks_dim1, nblks_dim2, ...])
     partitions the single-dimensional or multi-dimensional range with its
     size specified in row vector whole_size, into multiple blocks, and
     outputs the partition structure via PS. PS is a struct-array, with
     d entries, where d is the dimension of the whole array. Each entry
     of PS is a struct with two fields: sinds and einds, which are
     row vectors respectively denoting the start indices and end indices
     of sequential blocks along the dimension.
     By this syntax, the user can specify the number of blocks along each
     dimension by nblks_dim1, nblks_dim2, .... They are all integers.

   - PS = slpartition(whole_size, 'maxblksize', mbs_dim1, mbs_dim2, ...)
     PS = slpartition(whole_size, 'maxblksize', [mbs_dim1, mbs_dim2, ...])
     By this syntax, the user can specify the maximum block sizes along
     each dimension by mbs_dim1, mbs_dim2, .... They are all integers.

   - PS = slpartition(whole_size, 'blksizes', blksizes_dim1, blksizes_dim2, ...)
     By this syntax, the user can specify the block sizes of all individual
     blocks in all dimensions. blksizes_dim1, blksizes_dim2, ...
     are integer vectors, with each entry specifying the length of a block
     in along some dimension.

   - PS = slpartition(whole_size, 'startinds', startinds_dim1, startinds_dim2, ...);
     PS = slpartition(whole_size, 'endinds', endinds_dim1, endinds_dim2, ...);
     By this syntax, the user can specify the start indices or end indices
     of the blocks in all dimensions. startinds_dim1, startinds_dim2, ...
     are integer vectors, with entry specifying the start index of a block
     along a certain dimension. Likewise, endinds_dim1, endinds_dim2, ...
     are also integer vectors, with entry specifying the ending index of 
     a block along a certain dimension.

 $ Remarks $
   - For the dimension, corresponding parameter is not given, the dimension
     is considered to be partitioned into multiple unit blocks with
     block length equaling 1 along that dimension.

 $ History $
   - Created by Dahua Lin on Dec 7th, 2005
   - Modified by Dahua Lin on Sep 10, 2006
       - make minor change to eliminate warnings

CROSS-REFERENCE INFORMATION ^

This function calls:
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:
  • slimgsetprep SLIMGSETPREP organizes the images in a MATLAB friendly way
  • slfindnn SLFINDNN Finds the nearest neighbors using specified strategy
  • slcovlarge SLCOVLARGE Computes large covariance matrix using memory-efficient way
  • slgaussest SLGAUSSEST Estimates the Gaussian models from samples
  • slgaussrnd SLGAUSSRND Generates random samples from Gaussian models
  • slequalpar2D SLEQUALPAR Partition a 2D array with balances for width and height

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function PS = slpartition(whole_size, spec_item, varargin)
0002 %SLPARTITION Partition a range into blocks in a specified manner
0003 %
0004 % $ Syntax $
0005 %   - PS = slpartition(whole_size, 'numblks', nblks_dim1, nblks_dim2, ...);
0006 %   - PS = slpartition(whole_size, 'numblks', [nblks_dim1, nblks_dim2, ...]);
0007 %   - PS = slpartition(whole_size, 'maxblksize', mbs_dim1, mbs_dim2, ...);
0008 %   - PS = slpartition(whole_size, 'maxblksize', [mbs_dim1, mbs_dim2, ...]);
0009 %   - PS = slpartition(whole_size, 'blksizes', blksizes_dim1, blksizes_dim2, ...);
0010 %   - PS = slpartition(whole_size, 'startinds', startinds_dim1, startinds_dim2, ...);
0011 %   - PS = slpartition(whole_size, 'endinds', endinds_dim1, endinds_dim2, ...);
0012 %
0013 % $ Description $
0014 %
0015 %   - PS = slpartition(whole_size, 'numblks', nblks_dim1, nblks_dim2, ...)
0016 %     PS = slpartition(whole_size, 'numblks', [nblks_dim1, nblks_dim2, ...])
0017 %     partitions the single-dimensional or multi-dimensional range with its
0018 %     size specified in row vector whole_size, into multiple blocks, and
0019 %     outputs the partition structure via PS. PS is a struct-array, with
0020 %     d entries, where d is the dimension of the whole array. Each entry
0021 %     of PS is a struct with two fields: sinds and einds, which are
0022 %     row vectors respectively denoting the start indices and end indices
0023 %     of sequential blocks along the dimension.
0024 %     By this syntax, the user can specify the number of blocks along each
0025 %     dimension by nblks_dim1, nblks_dim2, .... They are all integers.
0026 %
0027 %   - PS = slpartition(whole_size, 'maxblksize', mbs_dim1, mbs_dim2, ...)
0028 %     PS = slpartition(whole_size, 'maxblksize', [mbs_dim1, mbs_dim2, ...])
0029 %     By this syntax, the user can specify the maximum block sizes along
0030 %     each dimension by mbs_dim1, mbs_dim2, .... They are all integers.
0031 %
0032 %   - PS = slpartition(whole_size, 'blksizes', blksizes_dim1, blksizes_dim2, ...)
0033 %     By this syntax, the user can specify the block sizes of all individual
0034 %     blocks in all dimensions. blksizes_dim1, blksizes_dim2, ...
0035 %     are integer vectors, with each entry specifying the length of a block
0036 %     in along some dimension.
0037 %
0038 %   - PS = slpartition(whole_size, 'startinds', startinds_dim1, startinds_dim2, ...);
0039 %     PS = slpartition(whole_size, 'endinds', endinds_dim1, endinds_dim2, ...);
0040 %     By this syntax, the user can specify the start indices or end indices
0041 %     of the blocks in all dimensions. startinds_dim1, startinds_dim2, ...
0042 %     are integer vectors, with entry specifying the start index of a block
0043 %     along a certain dimension. Likewise, endinds_dim1, endinds_dim2, ...
0044 %     are also integer vectors, with entry specifying the ending index of
0045 %     a block along a certain dimension.
0046 %
0047 % $ Remarks $
0048 %   - For the dimension, corresponding parameter is not given, the dimension
0049 %     is considered to be partitioned into multiple unit blocks with
0050 %     block length equaling 1 along that dimension.
0051 %
0052 % $ History $
0053 %   - Created by Dahua Lin on Dec 7th, 2005
0054 %   - Modified by Dahua Lin on Sep 10, 2006
0055 %       - make minor change to eliminate warnings
0056 %
0057 
0058 %% parse and verify input arguments
0059 if nargin < 3
0060     raise_lackinput('slpartition', 3);
0061 end
0062 dim_whole = length(whole_size);
0063 if ~isequal(size(whole_size), [1, dim_whole])
0064     error('sltoolbox:notsizevec', ...
0065         'The size vector whole_size should be a row vector');
0066 end
0067 if any(whole_size <= 0)
0068     error('sltoolbox:emptyarray', ...
0069         'The whole_size corresponds to an empty array');
0070 end
0071 % compute the actual dimension
0072 dim_whole = find(whole_size > 1, 1, 'last');
0073 whole_size = whole_size(1:dim_whole);
0074 
0075 %% Compute
0076 
0077 % initialize the struct
0078 PS = struct('sinds', cell(dim_whole, 1), 'einds', []);
0079 
0080 % process
0081 switch spec_item
0082     case 'numblks'
0083         
0084         if numel(varargin{1}) > 1
0085             G = varargin{1};
0086         else
0087             G = cell2mat(varargin);
0088         end
0089         nG = length(G);
0090         
0091         for d = 1 : dim_whole            
0092             if d <= nG
0093                 [PS(d).sinds, PS(d).einds] = ...
0094                     partition1d_numblks(whole_size(d), G(d));
0095             else
0096                 [PS(d).sinds, PS(d).einds] = ...
0097                     partition1d_full(whole_size(d));
0098             end
0099         end        
0100         
0101     case 'maxblksize'
0102         
0103         if numel(varargin{1}) > 1
0104             G = varargin{1};
0105         else
0106             G = cell2mat(varargin);
0107         end
0108         nG = length(G);
0109         
0110         for d = 1 : dim_whole            
0111             if d <= nG
0112                 [PS(d).sinds, PS(d).einds] = ...
0113                     partition1d_maxblksize(whole_size(d), G(d));
0114             else
0115                 [PS(d).sinds, PS(d).einds] = ...
0116                     partition1d_full(whole_size(d));
0117             end
0118         end     
0119         
0120     case 'blksizes'
0121         
0122         G = varargin;
0123         nG = length(G);
0124         
0125         for d = 1 : dim_whole            
0126             if d <= nG
0127                 [PS(d).sinds, PS(d).einds] = ...
0128                     partition1d_blksizes(whole_size(d), G{d});
0129             else
0130                 [PS(d).sinds, PS(d).einds] = ...
0131                     partition1d_full(whole_size(d));
0132             end
0133         end             
0134         
0135     case 'startinds'
0136         
0137         G = varargin;
0138         nG = length(G);
0139         
0140         for d = 1 : dim_whole            
0141             if d <= nG
0142                 [PS(d).sinds, PS(d).einds] = ...
0143                     partition1d_startinds(whole_size(d), G{d});
0144             else
0145                 [PS(d).sinds, PS(d).einds] = ...
0146                     partition1d_full(whole_size(d));
0147             end
0148         end         
0149         
0150     case 'endinds'
0151         
0152         G = varargin;
0153         nG = length(G);
0154         
0155         for d = 1 : dim_whole            
0156             if d <= nG
0157                 [PS(d).sinds, PS(d).einds] = ...
0158                     partition1d_endinds(whole_size(d), G{d});
0159             else
0160                 [PS(d).sinds, PS(d).einds] = ...
0161                     partition1d_full(whole_size(d));
0162             end
0163         end 
0164         
0165     otherwise
0166         error('sltoolbox:invalid_item', ...
0167             'Invalid partition item name: %s', spec_item);
0168 end
0169 
0170 
0171 %% ============ single-dimension partition functions =============
0172 
0173 function check_validity(siz, s, e)
0174 
0175 if length(s) ~= length(e)
0176     error('sltoolbox:invalid_partition', ...
0177         'The number of start indices and that of end indices do not consist');
0178 end
0179 if any(s > e)
0180     error('sltoolbox:invalid_partition', ...
0181         'Found some starting indices is larger than corresponding end indices');
0182 end
0183 if ~isequal(e(1:end-1)+1, s(2:end))
0184     error('sltoolbox:invalid_partition', ...
0185         'The regions are not contingent');
0186 end
0187 if s(1) ~= 1
0188     error('sltoolbox:invalid_partition', ...
0189         'The first starting index is not equal to 1');
0190 end
0191 if e(end) ~= siz
0192     error('sltoolbox:invalid_partition', ...
0193         'The last end index is not equal to dimension');
0194 end
0195 
0196 
0197 function [s, e] = partition1d_full(siz)
0198 
0199 s = 1:siz;
0200 e = s;
0201 check_validity(siz, s, e);
0202 
0203 
0204 function [s, e] = partition1d_numblks(siz, nblks)
0205 
0206 if nblks > siz
0207     error('The number of blocks should not exceed the whole dimension');
0208 end
0209 b = round(linspace(1, siz+1, nblks+1));
0210 s = b(1:nblks);
0211 e = b(2:nblks+1)-1;
0212 check_validity(siz, s, e);
0213 
0214 
0215 function [s, e] = partition1d_maxblksize(siz, mbs)
0216 
0217 nblks = ceil(siz / mbs);
0218 if nblks == 1
0219     blksizes = siz;
0220 else
0221     blksizes = [mbs * ones(1, nblks-1), siz - mbs * (nblks-1)];
0222 end
0223 [s, e] = partition1d_blksizes(siz, blksizes);
0224 
0225 
0226 function [s, e] = partition1d_blksizes(siz, blksizes)
0227 
0228 if sum(blksizes) ~= siz
0229     error('The total block sizes is not equal to the whole size');
0230 end
0231 if any(blksizes <= 0)
0232     error('Some block sizes are less than or equal to zero');
0233 end
0234 e = cumsum(blksizes);
0235 s = [1, e(1:end-1)+1];
0236 check_validity(siz, s, e);
0237 
0238 
0239 function [s, e] = partition1d_startinds(siz, startinds)
0240 
0241 if startinds(1) ~= 1
0242     error('The first starting index should be equal to 1');
0243 end
0244 if startinds(end) > siz
0245     error('The last starting index should not exceeds the whole size');
0246 end
0247 if any(diff(startinds) <= 0)
0248     error('The order of starting indices is incorrect');
0249 end
0250 
0251 s = startinds;
0252 e = [startinds(2:end)-1, siz];
0253 check_validity(siz, s, e);
0254 
0255 
0256 function [s, e] = partition1d_endinds(siz, endinds)
0257 
0258 if endinds(1) < 1
0259     error('The first ending index should be not less than 1');
0260 end
0261 if endinds(end) ~= siz
0262     error('The last ending index should be equal to the whole size');
0263 end
0264 if any(diff(endinds) <= 0)
0265     error('The order of ending indices is incorrect');
0266 end
0267 
0268 e = endinds;
0269 s = [1, e(1:end-1)+1];
0270 check_validity(siz, s, e);
0271     
0272 
0273 
0274 
0275 
0276 
0277 
0278 
0279 
0280 
0281 
0282 
0283 
0284 
0285 
0286 
0287

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com