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 sltrace2x2
Home > sltoolbox > smallmat > sltrace2x2.m

sltrace2x2

PURPOSE ^

SLTRACE2X2 Computes the trace of 2 x 2 matrices in a fast way

SYNOPSIS ^

function r = sltrace2x2(Ms)

DESCRIPTION ^

SLTRACE2X2 Computes the trace of 2 x 2 matrices in a fast way

 $ Syntax $
   - r = sltrace2x2(Ms)

 $ Arguments $
   - Ms:       the 2 x 2 matrix (matrices)
   - r:        the resultant trace(s)

 $ Description $
   - r = sltrace2x2(Ms) computes the trace of 2x2 matrices. If Ms is
     a 2 x 2 matrix, then r is a scalar representing its trace. 
     Or, if Ms is a 2 x 2 x ... array, then r would be a ... array 
     storing the trace of all 2 x 2 matrices.

 $ Remarks $
   - The function uses the following formula for fast calculation of
     trace of 2 x 2 matrices:
       det = a11 + a22

 $ History $
   - Created by Dahua Lin on Apr 22nd, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:

SOURCE CODE ^

0001 function r = sltrace2x2(Ms)
0002 %SLTRACE2X2 Computes the trace of 2 x 2 matrices in a fast way
0003 %
0004 % $ Syntax $
0005 %   - r = sltrace2x2(Ms)
0006 %
0007 % $ Arguments $
0008 %   - Ms:       the 2 x 2 matrix (matrices)
0009 %   - r:        the resultant trace(s)
0010 %
0011 % $ Description $
0012 %   - r = sltrace2x2(Ms) computes the trace of 2x2 matrices. If Ms is
0013 %     a 2 x 2 matrix, then r is a scalar representing its trace.
0014 %     Or, if Ms is a 2 x 2 x ... array, then r would be a ... array
0015 %     storing the trace of all 2 x 2 matrices.
0016 %
0017 % $ Remarks $
0018 %   - The function uses the following formula for fast calculation of
0019 %     trace of 2 x 2 matrices:
0020 %       det = a11 + a22
0021 %
0022 % $ History $
0023 %   - Created by Dahua Lin on Apr 22nd, 2006
0024 %
0025 
0026 %% parse and verify input arguments
0027 
0028 if size(Ms, 1) ~= 2 || size(Ms, 2) ~= 2
0029     error('sltoolbox:invalidarg', 'Ms should be set of 2 x 2 matrices');
0030 end
0031 
0032 %% compute
0033 
0034 if ndims(Ms) == 2       % single matrix
0035     r = Ms(1) + Ms(4);
0036     
0037 else                    % a set of matrices
0038     r = Ms(1,1,:) + Ms(2,2,:);
0039     
0040     siz = size(Ms);
0041     
0042     if ndims(Ms) == 3
0043         siz_r = [siz(3), 1];
0044     else
0045         siz_r = siz(3:end);
0046     end
0047     
0048     r = reshape(r, siz_r);
0049     
0050 end
0051

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

Contact us at files@mathworks.com