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 slmul
Home > sltoolbox > core > slmul.m

slmul

PURPOSE ^

SLMUL Multiply a sub-array along some dimensions to an array

SYNOPSIS ^

function Ar = slmul(A0, As, d)

DESCRIPTION ^

SLMUL Multiply a sub-array along some dimensions to an array

 $ Syntax $
   - Ar = slmul(A0, As)
   - Ar = slmul(A0, As, d)

 $ Arguments $
   - A0:           the original array
   - v:            the sub-array to be multiplied to the array
   - Ar:           the resultant array
   - d:            the dimension along which the vector is multiplied

 $ Description $
   - Ar = slmul(A0, As) multiplies the sub-array As to the array A0 along 
     auto-selected dimensions. The dimensions are identified by the 
     dimension of As with size larger than 1. If As is a scalar, then 
     all elements of A0 will be multiplied As.
   
   - Ar = slmul(A0, As, d) multiplies the sub-array As to the array A0 along
     the dimensions specified by d. 

 $ Remarks $
   # An empty As is allowed. In such case, the original array A0 will
     be output, i.e. Ar = A0.
   # The sizes of dimensions along which the sub-array is multiplied should
     match that of A0, otherwise, an error will be raised.
   # By specifying the dimensions through d, the speed can be accelerated.

 $ Examples $
   - Multiply a vector to a matrix.
     \{
         A = [1 2 3; 4 5 6];
         v = [2; 5];
         Ar = slmul(A, v)
     
         Ar = 

             2     4     6         
            20    25    30

     \}
     It is equivalent to sladd(A, v, 1).

  - Multiply a plane to a matrix
    \{
        A1 = [1 2 3; 4 5 6];
        A2 = [7 8 9; 10 11 12];
        A = cat(3, A1, A2);
        v1 = [10; 20];
        v2 = [30; 40];
        As = cat(3, v1, v2)

        Ar(:, :, 1) = 
            
            10     20    30
            80    100   120

        Ar(:, :, 2) = 

            210    240   270
            400    440   480

    \}

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

CROSS-REFERENCE INFORMATION ^

This function calls:
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:
  • slinv2x2 SLINV2X2 Computes inverse matrices for 2 x 2 matrices in a fast way

SOURCE CODE ^

0001 function Ar = slmul(A0, As, d)
0002 %SLMUL Multiply a sub-array along some dimensions to an array
0003 %
0004 % $ Syntax $
0005 %   - Ar = slmul(A0, As)
0006 %   - Ar = slmul(A0, As, d)
0007 %
0008 % $ Arguments $
0009 %   - A0:           the original array
0010 %   - v:            the sub-array to be multiplied to the array
0011 %   - Ar:           the resultant array
0012 %   - d:            the dimension along which the vector is multiplied
0013 %
0014 % $ Description $
0015 %   - Ar = slmul(A0, As) multiplies the sub-array As to the array A0 along
0016 %     auto-selected dimensions. The dimensions are identified by the
0017 %     dimension of As with size larger than 1. If As is a scalar, then
0018 %     all elements of A0 will be multiplied As.
0019 %
0020 %   - Ar = slmul(A0, As, d) multiplies the sub-array As to the array A0 along
0021 %     the dimensions specified by d.
0022 %
0023 % $ Remarks $
0024 %   # An empty As is allowed. In such case, the original array A0 will
0025 %     be output, i.e. Ar = A0.
0026 %   # The sizes of dimensions along which the sub-array is multiplied should
0027 %     match that of A0, otherwise, an error will be raised.
0028 %   # By specifying the dimensions through d, the speed can be accelerated.
0029 %
0030 % $ Examples $
0031 %   - Multiply a vector to a matrix.
0032 %     \{
0033 %         A = [1 2 3; 4 5 6];
0034 %         v = [2; 5];
0035 %         Ar = slmul(A, v)
0036 %
0037 %         Ar =
0038 %
0039 %             2     4     6
0040 %            20    25    30
0041 %
0042 %     \}
0043 %     It is equivalent to sladd(A, v, 1).
0044 %
0045 %  - Multiply a plane to a matrix
0046 %    \{
0047 %        A1 = [1 2 3; 4 5 6];
0048 %        A2 = [7 8 9; 10 11 12];
0049 %        A = cat(3, A1, A2);
0050 %        v1 = [10; 20];
0051 %        v2 = [30; 40];
0052 %        As = cat(3, v1, v2)
0053 %
0054 %        Ar(:, :, 1) =
0055 %
0056 %            10     20    30
0057 %            80    100   120
0058 %
0059 %        Ar(:, :, 2) =
0060 %
0061 %            210    240   270
0062 %            400    440   480
0063 %
0064 %    \}
0065 %
0066 % $ History $
0067 %   - Created by Dahua Lin on Nov 18th, 2005
0068 %
0069 
0070 %% parse and verify input
0071 if nargin < 2
0072     raise_lackinput('slmul', 2);
0073 end
0074 if isempty(As)
0075     Ar = A0;
0076     return;
0077 end
0078 if ndims(As) > ndims(A0)
0079     error('sltoolbox:dimoverflow', ...
0080         'The dimension of As should not be larger than that of A0');
0081 end
0082 if nargin < 3 || isempty(d)
0083     % d is not specified, automatically determine d
0084     d = find(size(As) > 1);
0085 end
0086 siz_A0 = size(A0);
0087 siz_As = size(As);
0088 if ~isequal(siz_A0(d), siz_As(d))
0089     error('sltoolbox:dimmismatch', ...
0090         'The dimensions of As does not match that of A0 in the dimensions to be multiplied');
0091 end
0092 
0093 %% compute
0094 siz_A0(d) = 1;
0095 Ar = A0 .* repmat(As, siz_A0);
0096 
0097     
0098 
0099 
0100

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

Contact us at files@mathworks.com