Home > atmlab > math > getdims.m

getdims

PURPOSE ^

GETDIMS Extract data from specified dimensions

SYNOPSIS ^

function B = getdims(A,dims)

DESCRIPTION ^

 GETDIMS   Extract data from specified dimensions

    The function allows a simple way to remove singleton dimensions and change
    dimensionality of data. This including changing the order of
    dimensions.

    The dimensions to keep are selected by *dims*. The size of *A* for
    dimensions not included in *dims* must be 1, but *dims* can include
    singleton dimensions. It is no demand that *dims* is sorted.

    For example:
       size(getdims(rand(1,2,1,4,1),[4 3 2])) 
    gives 
       4 1 2

 FORMAT   B = getdims( A, dims )
        
 OUT   B      Extracted data.
 IN    A      Original data.
       dims   Dimensions to extract.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

getdims.m

SOURCE CODE ^

0001 % GETDIMS   Extract data from specified dimensions
0002 %
0003 %    The function allows a simple way to remove singleton dimensions and change
0004 %    dimensionality of data. This including changing the order of
0005 %    dimensions.
0006 %
0007 %    The dimensions to keep are selected by *dims*. The size of *A* for
0008 %    dimensions not included in *dims* must be 1, but *dims* can include
0009 %    singleton dimensions. It is no demand that *dims* is sorted.
0010 %
0011 %    For example:
0012 %       size(getdims(rand(1,2,1,4,1),[4 3 2]))
0013 %    gives
0014 %       4 1 2
0015 %
0016 % FORMAT   B = getdims( A, dims )
0017 %
0018 % OUT   B      Extracted data.
0019 % IN    A      Original data.
0020 %       dims   Dimensions to extract.
0021 
0022 % 2007-10-17   Created by Patrick Eriksson.
0023 
0024 
0025 function B = getdims(A,dims)
0026 
0027 %- Check input
0028 %
0029 ndim = dimens( A );
0030 %
0031 if ~isnumeric(dims) | ~isvector(dims)  |  any( iswhole(dims) ~= 1 ) 
0032   error( 'Input *dims* must be a numeric vector of integers.' );
0033 end
0034 %
0035 if length(unique(dims)) ~= length(dims)
0036   error( 'Duplicates of some dimension found in *dims*.' );
0037 end
0038 %
0039 for id = 1 : ndim
0040   if ~any(id==dims)  &  size(A,id)>1
0041     error( 'Input *A* must have size 1 for dimensions not part of *dims*.' );
0042   end
0043 end
0044 
0045 
0046 %- Sort dims
0047 %
0048 if issorted(dims)
0049   sorted = 1;
0050 else
0051   sorted       = 0;
0052   [dims,order] = sort( dims );
0053 end
0054 
0055 %- Create reshape vector
0056 %
0057 n = ones( 1, max([2 length(dims)]) ); % reshape demands 2 values
0058 %
0059 for id = 1 : length(dims)
0060   n(id) = size(A,dims(id));
0061 end
0062 
0063 
0064 %- Reshape
0065 %
0066 B = reshape( A, n );
0067 
0068 
0069 %- Permute to follow original order of *dims*
0070 %
0071 if ~sorted
0072   % Determine "back-sorting" order
0073   [u,order] = sort( order );
0074   B         = permute( B, order );
0075 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005