//function HC=hist_cost_2(BH1,BH2); //% HC=hist_cost_2(BH1,BH2); //% //% same as hist_cost.m but BH1 and BH2 can be of different lengths public Matrix HistCost(Matrix BH1, Matrix BH2) { if (BH1.Columns != BH2.Columns) throw new Exception("槽数不等没法比!"); //[nsamp1,nbins]=size(BH1); //[nsamp2,nbins]=size(BH2); int nbins = BH1.Columns; //BH1n=BH1./repmat(sum(BH1,2)+eps,[1 nbins]); //%应该是repmat的结果 [sum(BH1,2),sum(BH1,2),sum(BH1,2),sum(BH1,2),sum(BH1,2).....] var BH1n = BH1.PointDivide((BH1.Sum2() + Epsilons(nsamp1, 1)).RepMat(1, nbins)); //BH2n=BH2./repmat(sum(BH2,2)+eps,[1 nbins]); var BH2n = BH2.PointDivide((BH2.Sum2() + Epsilons(nsamp2, 1)).RepMat(1, nbins)); //tmp1=repmat(permute(BH1n,[1 3 2]),[1 nsamp2 1]); % permute似乎用来改变维的顺序 //tmp2=repmat(permute(BH2n',[3 2 1]),[nsamp1 1 1]); //HC=0.5*sum(((tmp1-tmp2).^2)./(tmp1+tmp2+eps),3); // 不知道怎么弄,猜吧 DenseMatrix cost = Zeros(BH1.Rows, BH2.Rows); for (int i = 0; i < BH1.Rows; ++i) { // BH1的每一行 var BH1nrow = BH1n.GetRow(i); for (int j = 0; j < BH2.Rows; ++j) { // BH2的每一行 var BH2nrow = BH2n.GetRow(j); double costij = 0; for (int k = 0; k < nbins; ++k) { //double tmp1 = BH1n[i, k], tmp2 = BH2n[j, k]; double tmp1 = BH1nrow[k], tmp2 = BH2nrow[k]; costij += ((tmp1 - tmp2) * (tmp1 - tmp2)) / (tmp1 + tmp2 + Epsilon); } cost[i, j] = costij * 0.5; } } return cost; }