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 sltensor_fold
Home > sltoolbox > tensor > sltensor_fold.m

sltensor_fold

PURPOSE ^

SLTENSOR_FOLD Folds a matrix into a tensor

SYNOPSIS ^

function T = sltensor_fold(M, dims, k)

DESCRIPTION ^

SLTENSOR_FOLD Folds a matrix into a tensor

 $ Syntax $
   - T = sltensor_fold(M, dims, k)

 $ Arguments $
   - M:                the unfolded matrix of the tensor
   - dims:             the dimension sizes of the tensor
   - k:                the dimension along which the tensor is folded
   - T:                the resultant tensor

 $ Description $
   - T = sltensor_fold(M, dims, k) folds the matrix M to a tensor with
   its mode dimensions specified in dims along the k-th mode.

 $ History $
   - Created by Dahua Lin on Dec 17th, 2005

CROSS-REFERENCE INFORMATION ^

This function calls:
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:

SOURCE CODE ^

0001 function T = sltensor_fold(M, dims, k)
0002 %SLTENSOR_FOLD Folds a matrix into a tensor
0003 %
0004 % $ Syntax $
0005 %   - T = sltensor_fold(M, dims, k)
0006 %
0007 % $ Arguments $
0008 %   - M:                the unfolded matrix of the tensor
0009 %   - dims:             the dimension sizes of the tensor
0010 %   - k:                the dimension along which the tensor is folded
0011 %   - T:                the resultant tensor
0012 %
0013 % $ Description $
0014 %   - T = sltensor_fold(M, dims, k) folds the matrix M to a tensor with
0015 %   its mode dimensions specified in dims along the k-th mode.
0016 %
0017 % $ History $
0018 %   - Created by Dahua Lin on Dec 17th, 2005
0019 %
0020 
0021 %% parse and verify
0022 if nargin < 2
0023     raise_lackinput('sltensor_fold', 3);
0024 end
0025 n = length(dims);
0026 if k < 1
0027     error('sltoolbox:invalidarg', ...
0028         'The mode index should be positive');
0029 elseif k > n
0030     error('sltoolbox:argmismatch', ...
0031         'The mode index exceeds the tensor order');
0032 end
0033 if numel(M) ~= prod(dims)
0034     error('sltoolbox:argmismatch', ...
0035         'The size of matrix and the dimensions of tensor do not match');
0036 end
0037 
0038 %% compute
0039 if k == 1
0040     T = reshape(M, dims);
0041 else
0042     if k < n
0043         pdims = [k, 1:k-1, k+1:n];
0044     else
0045         pdims = [k, 1:k-1];
0046     end
0047     T = reshape(M, dims(pdims));
0048     T = ipermute(T, pdims);
0049 end
0050

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

Contact us at files@mathworks.com