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 slexpand
Home > sltoolbox > utils > slexpand.m

slexpand

PURPOSE ^

SLEXPAND Expand a set to multiple instance

SYNOPSIS ^

function A = slexpand(nums, U)

DESCRIPTION ^

SLEXPAND Expand a set to multiple instance

 $ Syntax $
   - A = slexpand(nums)
   - A = slexpand(nums, U)

 $ Arguments $
   - nums:           the numbers of instances for entities
   - U:              the unique set of entities (numeric vector or cell arr)
   - A:              the expanded array

 $ Description $
   - A = slexpand(nums) expands the one-based labels by numbers specified
     in nums.

   - A = slexpand(nums, U) expands the entity-set U by numbers specified
     in nums.

 $ Remarks $
   # nums can be either column vector or row vector. Then, A would be
     column array or row array correspondingly.
   # U can be numeric array or cell array. Then A would be numeric array
     or cell array correspondingly.
   # If U is not specified, it is equivalent to set U = [1, 2, ...];

 $ Examples $
   - Expand one-based labels,
     \{
          A = slexpand([3 2 4])

          A = 
               1     1     1     2     2     3     3     3     3
     \}

   - Expand a designated set
     \{
          A = slexpand([2; 3], [10 20])

          A = 
               10
               10
               20
               20
               20
     \}

   - Expand a cell array
     \{
         A = slexpand([2 3], {'a', 'b'})

         A = 
              'a'    'a'    'b'    'b'    'b'
     \}

 $ History $
   - Created by Dahua Lin on Nov 19th, 2005

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:

SOURCE CODE ^

0001 function A = slexpand(nums, U)
0002 %SLEXPAND Expand a set to multiple instance
0003 %
0004 % $ Syntax $
0005 %   - A = slexpand(nums)
0006 %   - A = slexpand(nums, U)
0007 %
0008 % $ Arguments $
0009 %   - nums:           the numbers of instances for entities
0010 %   - U:              the unique set of entities (numeric vector or cell arr)
0011 %   - A:              the expanded array
0012 %
0013 % $ Description $
0014 %   - A = slexpand(nums) expands the one-based labels by numbers specified
0015 %     in nums.
0016 %
0017 %   - A = slexpand(nums, U) expands the entity-set U by numbers specified
0018 %     in nums.
0019 %
0020 % $ Remarks $
0021 %   # nums can be either column vector or row vector. Then, A would be
0022 %     column array or row array correspondingly.
0023 %   # U can be numeric array or cell array. Then A would be numeric array
0024 %     or cell array correspondingly.
0025 %   # If U is not specified, it is equivalent to set U = [1, 2, ...];
0026 %
0027 % $ Examples $
0028 %   - Expand one-based labels,
0029 %     \{
0030 %          A = slexpand([3 2 4])
0031 %
0032 %          A =
0033 %               1     1     1     2     2     3     3     3     3
0034 %     \}
0035 %
0036 %   - Expand a designated set
0037 %     \{
0038 %          A = slexpand([2; 3], [10 20])
0039 %
0040 %          A =
0041 %               10
0042 %               10
0043 %               20
0044 %               20
0045 %               20
0046 %     \}
0047 %
0048 %   - Expand a cell array
0049 %     \{
0050 %         A = slexpand([2 3], {'a', 'b'})
0051 %
0052 %         A =
0053 %              'a'    'a'    'b'    'b'    'b'
0054 %     \}
0055 %
0056 % $ History $
0057 %   - Created by Dahua Lin on Nov 19th, 2005
0058 %
0059 
0060 %% parse and verify input arguments
0061 [d1, d2] = size(nums);
0062 if d1 == 1 % row vector
0063     c = d2;
0064     iscol = false;
0065 elseif d2 == 1
0066     c = d1;
0067     iscol = true;
0068 else
0069     error('sltoolbox:notvector', 'nums should be a vector');
0070 end
0071 if nargin < 2 || isempty(U)
0072     U = 1:c;
0073 end
0074 if numel(U) < c
0075     error('sltoolbox:notenoughelems', 'U should have at least c elements');
0076 end
0077 n = sum(nums(:));
0078 
0079 %% prepare container
0080 if isnumeric(U)
0081     if iscol
0082         A = zeros(n, 1);
0083     else
0084         A = zeros(1, n);
0085     end
0086 elseif iscell(U)
0087     if iscol
0088         A = cell(n, 1);
0089     else
0090         A = cell(1, n);
0091     end
0092 else
0093     error('sltoolbox:invalidtype', 'U should be a numeric array or a cell array');
0094 end
0095 
0096 %% execute expanding
0097 
0098 p2 = 0;
0099 for k = 1 : c
0100     p1 = p2 + 1;
0101     p2 = p2 + nums(k);
0102     
0103     A(p1:p2) = U(k);    
0104 end
0105 
0106 
0107 
0108     
0109 
0110 
0111 
0112 
0113

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

Contact us at files@mathworks.com