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

slallcombs

PURPOSE ^

SLALLCOMBS Generate all combination of numbers

SYNOPSIS ^

function A = slallcombs(nums)

DESCRIPTION ^

SLALLCOMBS Generate all combination of numbers

 $ Syntax $
   - A = slallcombs(nums)

 $ Arguments $
   - nums:         the numbers of component sets
   - A:            the resultant indices matrix

 $ Description $
   - A = slallcombs(nums) generates the set of all possible index-vectors
     for a d1 x d2 x ... x dK array, where d1, d2, ... dK are stored in 
     the K-dim vector nums. Then the resultant matrix A would be of size
     K x dK x ... d2 x d1.

 $ Remarks $
   # If the number in some dimensions equals zero, then an empty array
     will be returned.

 $ Examples $
   - Generate all indices for 3D array,
     \{
           A = slallcombs([3 2 4]);
     \}
     Then A is a 3 x 3 x 2 x 4 matrix, with 
     A(:, i3, i2, i1) = [i1 i2 i3]'

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

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:
  • slcartprod SLCARTPROD Get the Cartesian product of a series of sets
  • slgridsamples SLGRIDSAMPLES Generate the sample vectors on grid points

SOURCE CODE ^

0001 function A = slallcombs(nums)
0002 %SLALLCOMBS Generate all combination of numbers
0003 %
0004 % $ Syntax $
0005 %   - A = slallcombs(nums)
0006 %
0007 % $ Arguments $
0008 %   - nums:         the numbers of component sets
0009 %   - A:            the resultant indices matrix
0010 %
0011 % $ Description $
0012 %   - A = slallcombs(nums) generates the set of all possible index-vectors
0013 %     for a d1 x d2 x ... x dK array, where d1, d2, ... dK are stored in
0014 %     the K-dim vector nums. Then the resultant matrix A would be of size
0015 %     K x dK x ... d2 x d1.
0016 %
0017 % $ Remarks $
0018 %   # If the number in some dimensions equals zero, then an empty array
0019 %     will be returned.
0020 %
0021 % $ Examples $
0022 %   - Generate all indices for 3D array,
0023 %     \{
0024 %           A = slallcombs([3 2 4]);
0025 %     \}
0026 %     Then A is a 3 x 3 x 2 x 4 matrix, with
0027 %     A(:, i3, i2, i1) = [i1 i2 i3]'
0028 %
0029 % $ History $
0030 %   - Created by Dahua Lin on Nov 19th, 2005
0031 %
0032 
0033 %% parse input arguments
0034 nums = nums(:);
0035 K = length(nums);
0036 n = prod(nums);
0037 if n == 0
0038     A = [];
0039     return;
0040 end
0041 
0042 %% prepare storage
0043 A = zeros(K, n);
0044 
0045 %% get organization tables
0046 cprods = cumprod(nums);
0047 n_grps = [1; cprods(1:end-1)];
0048 n_ins = cprods(end) ./ cprods;
0049 
0050 %% organize
0051 for i = 1 : K
0052     
0053     r = n_ins(i);
0054     s = nums(i);
0055     g = n_grps(i);
0056     
0057     P = (1:s);
0058     P = P(ones(r, 1), :);
0059     P = P(:);
0060     P = P(:, ones(1, g));
0061     
0062     A(i, :) = P(:)';
0063 end
0064 
0065 A = reshape(A, [K, nums(end:-1:1)']);
0066

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

Contact us at files@mathworks.com