I have tried to implement the customized calculation function for obtaining the kernel matrix, can we obtain a better implementation ?
sourceFeats = dlarray(rand(3,5),"CB");
targetFeats = dlarray(rand(3,10),"CB");
% Calculate the kernal matrix between samples
sourceFeats = stripdims(sourceFeats); % Strip dims for calculation
targetFeats = stripdims(targetFeats);
myKerFcn(sourceFeats.',targetFeats.')
ans =
5×10 dlarray
0.6859 0.8999 0.7779 0.8335 0.7132 0.7342 0.7626 0.7001 0.7402 0.7989
0.5545 0.6470 0.5867 0.6434 0.7192 0.6391 0.6703 0.7205 0.5736 0.6163
0.5322 0.7000 0.6616 0.6667 0.6249 0.6045 0.7382 0.6410 0.5758 0.6145
0.6306 0.7260 0.6293 0.8472 0.6317 0.5813 0.8341 0.6827 0.7631 0.7116
0.6501 0.7767 0.7213 0.7063 0.7355 0.7815 0.6605 0.6828 0.6479 0.7263
function pDist = myKerFcn(obsX,obsY)
arguments (Input)
obsX (:,:)
obsY (:,:)
end
arguments (Output)
pDist (:,:)
end
numX = size(obsX,1);
numY = size(obsY,1);
pDist=reshape(exp(-vecnorm(repmat(obsX,numY,1) - repelem(obsY,numX,1),2,2)/2),numX,numY);
end
