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

sltensor_multiply

PURPOSE ^

SLTENSOR_MULTIPLY Multiplies a tensor and a matrix

SYNOPSIS ^

function T2 = sltensor_multiply(T, varargin)

DESCRIPTION ^

SLTENSOR_MULTIPLY Multiplies a tensor and a matrix

 $ Syntax $
   - T2 = sltensor_multiply(T, M, k)
   - T2 = sltensor_multiply(T, Ms)
   - T2 = sltensor_multiply(T, Ms, ks)
   - T2 = sltensor_multiply(T, M1, k1, M2, k2, ...)

 $ Description $
   - T2 = sltensor_multiply(T, M, k) Computes the tensor multiplication between
   a tensor and a 2D matrix along the k-th mode.

   - T2 = sltensor_multiply(T, Ms) Sequentially multiplies the tensor with
   the elements in Ms, which is a cell array from the mode 1 to n. n is
   the number of matrices in Ms. It should be that n == ndims(T).

   - T2 = sltensor_multiply(T, Ms, ks) Sequentially multiplies the tensor
   with the matrices in the cell array Ms along the modes specified in
   corresponding element in ks. ks should be an array with the same size
   as Ms.

   - T2 = sltensor_multiply(T, M1, k1, M2, k2, ...) Sequentially multiplies 
   the tensor with the matrices M1, M2, ... along the modes k1, k2, ...

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:
  • sltensor_svd SLTENSOR_SVD Performs a Higher-Order SVD on a Tensor

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function T2 = sltensor_multiply(T, varargin)
0002 %SLTENSOR_MULTIPLY Multiplies a tensor and a matrix
0003 %
0004 % $ Syntax $
0005 %   - T2 = sltensor_multiply(T, M, k)
0006 %   - T2 = sltensor_multiply(T, Ms)
0007 %   - T2 = sltensor_multiply(T, Ms, ks)
0008 %   - T2 = sltensor_multiply(T, M1, k1, M2, k2, ...)
0009 %
0010 % $ Description $
0011 %   - T2 = sltensor_multiply(T, M, k) Computes the tensor multiplication between
0012 %   a tensor and a 2D matrix along the k-th mode.
0013 %
0014 %   - T2 = sltensor_multiply(T, Ms) Sequentially multiplies the tensor with
0015 %   the elements in Ms, which is a cell array from the mode 1 to n. n is
0016 %   the number of matrices in Ms. It should be that n == ndims(T).
0017 %
0018 %   - T2 = sltensor_multiply(T, Ms, ks) Sequentially multiplies the tensor
0019 %   with the matrices in the cell array Ms along the modes specified in
0020 %   corresponding element in ks. ks should be an array with the same size
0021 %   as Ms.
0022 %
0023 %   - T2 = sltensor_multiply(T, M1, k1, M2, k2, ...) Sequentially multiplies
0024 %   the tensor with the matrices M1, M2, ... along the modes k1, k2, ...
0025 %
0026 % $ History $
0027 %   - Created by Dahua Lin on Dec 17th, 2005
0028 %
0029 
0030 %% parse and verify input arguments
0031 if nargin < 2
0032     raise_lackinput('sltensot_multiply', 2);
0033 end
0034 n0 = ndims(T);
0035 if iscell(varargin{1});
0036     Ms = varargin{1};
0037     if nargin == 2
0038         n = numel(Ms);
0039         if n ~= n0
0040             error('sltoolbox:invalidarg', ...
0041                 'For the case no specifying mode indices, it should be n == ndims(T)');
0042         end
0043         ks = 1:n;
0044     elseif nargin == 3
0045         ks = varargin{2};
0046         if ~isequal(size(Ms), size(k))
0047             error('sltoolbox:argmismatch', ...
0048                 'The size of ks does not match that of Ms');
0049         end    
0050     else
0051         error('sltoolbox:invalidarg', ...
0052             'Invalid input arguments');
0053     end
0054 else
0055     if mod(length(varargin), 2) ~= 0
0056         error('sltoolbox:invalidarg', ...
0057             'Invalid input arguments');
0058     end
0059     Ms = varargin(1:2:end);
0060     ks = [varargin{2:2:end}];
0061     if numel(Ms) ~= numel(ks)
0062         error('sltoolbox:invalidarg', ...
0063             'Invalid input arguments');
0064     end
0065 end
0066 if any(ks(:) < 1)
0067     error('sltoolbox:invalidarg', ...
0068         'mode indices should all be positive integers');
0069 end
0070 maxk = max(ks(:));
0071 dims = size(T);
0072 if maxk > n0
0073     dims = [dims, ones(1, maxk-n0)];
0074 end
0075 nmat = numel(ks);
0076     
0077 %% compute
0078 if nmat == 1
0079     T2 = multiply_tensor_matrix(T, Ms{1}, ks, dims);
0080 else
0081     T2 = T;
0082     for i = 1 : nmat
0083         T2 = multiply_tensor_matrix(T2, Ms{i}, ks(i), dims);
0084     end
0085 end
0086     
0087 
0088 
0089 %% compute (by converting to matrix product)
0090 
0091 function T2 = multiply_tensor_matrix(T, M, k, dims)
0092 
0093 Tk = sltensor_unfold(T, k);
0094 T2 = M * Tk;
0095 clear Tk;
0096 dims(k) = size(M, 1);
0097 T2 = sltensor_fold(T2, dims, k);
0098 
0099 
0100 
0101 
0102

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

Contact us at files@mathworks.com