Home > helper > MatOps.m

MatOps

PURPOSE ^

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 classdef MatOps
0002     properties
0003         
0004         
0005     end
0006         
0007     methods
0008         %% Multiply an 2d array to an multi-d array element by element
0009         function Z = times_element(X, Y)
0010         % Z(:,:,i,j,...) = X(:,:,i,j,...) .* Y;
0011         %
0012         % X is a m-by-n-by-p-by-q-by... array
0013         % Y is a m-by-n array
0014         % Z is a m-by-n-by-p-by-q-by... array
0015         sx = size(X);
0016         Z = X .* repmat(Y, [1 1 sx(3:end) ] );
0017         
0018         end
0019         
0020         %% Multiply an 2d array to an multi-d array using left multiplication
0021         function Z = times_left(X, Y)
0022         % Z(:,:,i,j,...) = Y * X(:,:,i,j,...);
0023         %
0024         % X is a m-by-n-by-p-by-q-by... array
0025         % Y is a m-by-n array
0026         % Z is a m-by-n-by-p-by-q-by... array
0027         
0028         sx = size(X);
0029         sy = size(Y);
0030         Z  = reshape(Y * X(:,:), [sy(1) sx(2: end)]);
0031         end
0032         
0033         %% Multiply an 2d array to an multi-d array using right multiplication
0034         function Z = times_right( X, Y )
0035         % Z(:,:,i,j,...) = X(:,:,i,j,...) * Y;
0036         %
0037         % X is a m-by-n-by-p-by-q-by... array
0038         % Y is a m-by-n array
0039         % Z is a m-by-n-by-p-by-q-by... array
0040         
0041         sx = size(X);
0042         sy = size(Y);
0043         dx = ndims(X);
0044         
0045         % Stacking  - vertical concatenation
0046         Xt = permute(X, [1 3:dx 2]);
0047         Xt = reshape(Xt, [prod(sx) / sx(2) sx(2)]);
0048         Z  = Xt * Y;
0049         % Unstacking
0050         Z  = reshape( Z, [sx(1) sx(3: dx) sy(2) ]);
0051         Z  = permute( Z, [1 dx 2:dx -1] );
0052         end
0053         
0054         %% Multiply matrix with every element of a vector
0055         function Z = times_vector( X, v)
0056         % Z = zeros(m,n,p);
0057         % for i = 1: p
0058         %   Z(:,:,i)  = X * v(i);
0059         % end
0060         %
0061         % X is a m-by-n array
0062         % v is a p length vector
0063         % Z is a m-by-n array
0064         
0065         Z = reshape( X(:) * v , [m n p]);
0066         
0067         end
0068         
0069         %% Multiply each 2D slide with corresponding element of a vector
0070         function Z = times_vector_slice( X, v)
0071         % Y = zeros(m, n, p);
0072         % for i = 1: p
0073         %    Y(:,:,i) = X(:,:,i) * v(i);
0074         % end
0075         %
0076         % X is a m-by-n array
0077         % v is a p length vector
0078         % Z is a m-by-n array
0079         
0080         Z = X .* repmat(reshape(v, [1 1 p]), [m n] );
0081         
0082         end
0083         
0084         %% Divide each 2D slice with the same matrix (element by element)
0085         function Z = divide_element(X, Y)
0086         % Z(:,:,i,j,...) = X(:,:,i,j,...) ./ Y;
0087         %
0088         % X is a m-by-n-by-p-by-q-by... array
0089         % Y is a m-by-n array
0090         % Z is a m-by-n-by-p-by-q-by... array
0091         
0092         sx = size(X);
0093         Z  = X ./ repmat(Y, [1 1 sx(3:end)]);
0094         end
0095         
0096         %% Divide each 2D slice with the same matrix( left)
0097         function Z = divide_left( X, Y)
0098         % Z(:,:,i,j,...) = Y \ X(:,:,i,j,...);
0099         %
0100         % X is a m-by-n-by-p-by-q-by... array
0101         % Y is a m-by-n array
0102         % Z is a m-by-n-by-p-by-q-by... array
0103         
0104         Z = reshape( Y \ X(:,:), size(X) );
0105         end
0106         
0107         %% Divide each 2D slice with the same matrix (right)
0108         function Z = divide_right( X, Y)
0109           % Z(:,:,i,j,...) = X(:,:,i,j,...) / Y;
0110         %
0111         % X is a m-by-n-by-p-by-q-by... array
0112         % Y is a m-by-n array
0113         % Z is a m-by-n-by-p-by-q-by... array
0114         
0115         sx = size(X);
0116         dx = ndims(X);
0117         v  = [1 3:dx 2];
0118         Xt = reshape(permute(X, v), [prod(sx)/sx(2) sx(2)]);
0119         Z  = Xt / Y;
0120         Z = ipermute( reshape( Z, sx(v)), v);
0121         end
0122         
0123         %% Euclidean distance between 2 points
0124         function d = euclidean_point(x, y)
0125         % x  - a point in n-d space
0126         % y  - another point in n-d space
0127         
0128         %d = norm(x - y);
0129         d = sqrt( sum( abs(x-y).^2 ));
0130         end
0131         
0132         %% Euclidean distance between matrix and a point
0133         function d = euclidean_vector( X, Y)
0134         % X  - m-by-p matrix present m point in p-d space
0135         % Y  - 1-by-p vector present a point in p-d space
0136         sx = size(X);
0137         d = sqrt( sum( abs(X - repmat(Y, [sx(1) 1])).^2, 2));
0138         end
0139         
0140         %% Euclidean distance between matrix and matrix
0141         function D = euclidean_matrix( X, Y)
0142         % X  - m-by-p matrix present m point in p-d space
0143         % Y  - n-by-p vector present n point in p-d space
0144         
0145         m = size(X,1);
0146         n = size(Y,1);
0147         
0148         i = (1:m).';              % index vector for X
0149         i = i(:, ones(1,n) );     % index matrix for X
0150         j = 1: n;                 % index vector for Y
0151         j = j(ones(1,m),:);       % index matrix for Y
0152         D = zeros(m,n);           % init output matrix
0153         D(:) = sqrt( sum(abs( X (i(:),:) - Y ( j(:),:)).^2, 2));
0154         end
0155         
0156         %% Mahalanobis distance
0157         function d = mahalanobis( X, Y)
0158         % The distance between two centroids each from a group of points
0159         nx = size(X, 1);
0160         ny = size(Y, 1);
0161         
0162         m  = sum(X,1) / nx;             % centroid (mean)
0163         Xc = X - m(ones(nx,1),:);       % distance to centroid of X
0164         C  = (Xc' * Xc) / (nx - 1);     % variance matrix
0165         Yc = Y - m(ones(ny,1),:);       % distance to centroid of Y
0166         d = sum( Yc/C .* Yc, 2);        % Mahalanobis distance
0167         end
0168     end
0169     
0170     
0171 end

Generated on Thu 17-Mar-2011 14:45:51 by m2html © 2005