0001 classdef MatOps
0002 properties
0003
0004
0005 end
0006
0007 methods
0008
0009 function Z = times_element(X, Y)
0010
0011
0012
0013
0014
0015 sx = size(X);
0016 Z = X .* repmat(Y, [1 1 sx(3:end) ] );
0017
0018 end
0019
0020
0021 function Z = times_left(X, Y)
0022
0023
0024
0025
0026
0027
0028 sx = size(X);
0029 sy = size(Y);
0030 Z = reshape(Y * X(:,:), [sy(1) sx(2: end)]);
0031 end
0032
0033
0034 function Z = times_right( X, Y )
0035
0036
0037
0038
0039
0040
0041 sx = size(X);
0042 sy = size(Y);
0043 dx = ndims(X);
0044
0045
0046 Xt = permute(X, [1 3:dx 2]);
0047 Xt = reshape(Xt, [prod(sx) / sx(2) sx(2)]);
0048 Z = Xt * Y;
0049
0050 Z = reshape( Z, [sx(1) sx(3: dx) sy(2) ]);
0051 Z = permute( Z, [1 dx 2:dx -1] );
0052 end
0053
0054
0055 function Z = times_vector( X, v)
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 Z = reshape( X(:) * v , [m n p]);
0066
0067 end
0068
0069
0070 function Z = times_vector_slice( X, v)
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080 Z = X .* repmat(reshape(v, [1 1 p]), [m n] );
0081
0082 end
0083
0084
0085 function Z = divide_element(X, Y)
0086
0087
0088
0089
0090
0091
0092 sx = size(X);
0093 Z = X ./ repmat(Y, [1 1 sx(3:end)]);
0094 end
0095
0096
0097 function Z = divide_left( X, Y)
0098
0099
0100
0101
0102
0103
0104 Z = reshape( Y \ X(:,:), size(X) );
0105 end
0106
0107
0108 function Z = divide_right( X, Y)
0109
0110
0111
0112
0113
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
0124 function d = euclidean_point(x, y)
0125
0126
0127
0128
0129 d = sqrt( sum( abs(x-y).^2 ));
0130 end
0131
0132
0133 function d = euclidean_vector( X, Y)
0134
0135
0136 sx = size(X);
0137 d = sqrt( sum( abs(X - repmat(Y, [sx(1) 1])).^2, 2));
0138 end
0139
0140
0141 function D = euclidean_matrix( X, Y)
0142
0143
0144
0145 m = size(X,1);
0146 n = size(Y,1);
0147
0148 i = (1:m).';
0149 i = i(:, ones(1,n) );
0150 j = 1: n;
0151 j = j(ones(1,m),:);
0152 D = zeros(m,n);
0153 D(:) = sqrt( sum(abs( X (i(:),:) - Y ( j(:),:)).^2, 2));
0154 end
0155
0156
0157 function d = mahalanobis( X, Y)
0158
0159 nx = size(X, 1);
0160 ny = size(Y, 1);
0161
0162 m = sum(X,1) / nx;
0163 Xc = X - m(ones(nx,1),:);
0164 C = (Xc' * Xc) / (nx - 1);
0165 Yc = Y - m(ones(ny,1),:);
0166 d = sum( Yc/C .* Yc, 2);
0167 end
0168 end
0169
0170
0171 end