| Description of slpartition |
slpartition
PURPOSE 
SLPARTITION Partition a range into blocks in a specified manner
SYNOPSIS 
function PS = slpartition(whole_size, spec_item, varargin)
DESCRIPTION 
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 
- function check_validity(siz, s, e)
- function [s, e] = partition1d_full(siz)
- function [s, e] = partition1d_numblks(siz, nblks)
- function [s, e] = partition1d_maxblksize(siz, mbs)
- function [s, e] = partition1d_blksizes(siz, blksizes)
- function [s, e] = partition1d_startinds(siz, startinds)
- function [s, e] = partition1d_endinds(siz, endinds)
SOURCE CODE 
0001 function PS = slpartition(whole_size, spec_item, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
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
0072 dim_whole = find(whole_size > 1, 1, 'last');
0073 whole_size = whole_size(1:dim_whole);
0074
0075
0076
0077
0078 PS = struct('sinds', cell(dim_whole, 1), 'einds', []);
0079
0080
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
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
|
|