Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vectorize the intersection kernel function in MATLAB?

I need to pre-compute the histogram intersection kernel matrices for using LIBSVM in MATLAB.

Assume x, y are two vectors. The kernel function is K(x, y) = sum(min(x, y)). In order to be efficient, the best practice in most cases is to vectorize the operations.

What I want to do is like calculate the kernel matrices like calculating the euclidean distance between two matrices, like pdist2(A, B, 'euclidean'). After defining function 'intKernel', I could calculate the intersection kernel by calling pdist2(A, B, intKernel).

I know the function 'pdist2' may be an option. But I have no idea how to write the self-defined distance function. While, I do not know how to code the intersection kernel between vector(1-by-M) and matrix(M-by-N) in one condense expression.

'repmat' may not be feasible, because the matrix is really large, let us say, 20000-by-360000.

Any help would be appreciated.

Regards, Peiyun

like image 581
Peiyun Avatar asked Jan 14 '14 13:01

Peiyun


1 Answers

I think pdist2 is a good option, so I help you to define your distance function.

According to the doc, the self-defined distance function must have 2 inputs: first one is a 1-by-N vector; second one is a M-by-N matrix (be careful of the order!).

To avoid the use of repmat which is indeed memory-consumant, you can use bsxfun to apply some basic operations on data with expansion over singleton dimensions. In your case, you can do the following thing:

distance_kernel = @(x,Y) sum(bsxfun(@min,x,Y),2);

Summation is done over the columns to get a column vector as output.

Then just call pdist2 and you are done.

like image 120
Bentoy13 Avatar answered Sep 18 '22 13:09

Bentoy13