Search code examples
matlabhistogramvectorizationintersectionbsxfun

MATLAB bsxfun with different non-singleton dimensions


I have two (50 train and 25 test) sets of histograms of size 42. (These numbers are arbitrary, they will be much bigger in reality, so I'm looking for an efficient way. Also the convention might be reverse in terms of transposes, so feel free to use any convention [feature x observation] or [observation x feature] )

So x1 is of size [42, 50] and x2 is [42, 25]. I want to calculate the histogram intersection kernel of size [50, 25]. By a histogram intersection kernel I mean the sum of the vector that contains the minimum elements of two histograms. How can I do this with MATLAB?

I tried k=sum(bsxfun(@min, x1,x2)); (with transpose variations) but I'm getting the error :

Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.

Thanks for any help!


Solution

  • You want an output of size [50,25] which is already summarized. The output of bsxfun should be of dimensions [50,25,42] which means all inputs must be of this size except for singleton dimensions. Your [42, 50] needs to be permute to [50,1,42] and the second input to [1,25,42]

    x1=rand(42,50);
    x2=rand(42,25);
    x1=permute(x1,[2,3,1]);
    x2=permute(x2,[3,2,1]);
    t=bsxfun(@min, x1,x2);
    k=sum(t,3);