This is for whom just want to use matrix-style in Matlab rather than looping. The thumb of rule is simple: you want simpler codes, so you must trade it off with more memory. Sometimes, elegant code and speed is your choice.
The formula of kernel is as follows:
Given two matrices and
in which
is the dimensionality of data. Says we want to compute the” Gram matrix” between
and
(strictly speaking, we call it Gram matrix iff
). In other word, we would like to compute
kernel value between every possible pairs
.
The Matlab code for that stuff is the following:
[m p] = size(A); n = size(B,1); aa = repmat(A,[1 n]); bb = repmat(reshape(B,1,[]),[m 1]); D = reshape(((aa-bb).^2)./(aa+bb),[m*n p]); D = reshape(sum(D,2),[m n]);
Advertisement