而不是使用 REPMAT 或建立索引来重复向量以匹配z的行,请考虑使用高效的 BSXFUN 功能:rbf(:,i) = exp( -g .* sum(bsxfun(@minus,z,x(i,:)).^2,2) );上面的内容显然遍历了x的每一行您可以再走一步,并使用 PDIST2 来计算z和x中每对行之间的欧几里得距离:%# some random dataX = rand(10,2);Z = rand(10,2);g = 0.5;%# one-line solutionrbf = exp(-g .* pdist2(Z,X,'euclidean').^2);现在矩阵中的每个值:rbf(i,j)对应于z(i,:)和x(j,:) 之间的函数值我为不同的方法计时,这是我使用的代码:%# some random dataN = 5000;X = rand(N,2);Z = rand(N,2);g = 0.5;%# PDIST2ticrbf1 = exp(-g .* pdist2(Z,X,'euclidean').^2);toc%# BSXFUN+loopticrbf2 = zeros(N,N);for j=1:N rbf2(:,j) = exp( -g .* sum(bsxfun(@minus,Z,X(j,:)).^2,2) );endtoc%# REPMAT+loopticrbf3 = zeros(N,N);for j=1:N rbf3(:,j) = exp( -g .* sum((Z-repmat(X(j,:),[N 1])).^2,2) );endtoc%# check if results are equalall( abs(rbf1(:)-rbf2(:)) < 1e-15 )all( abs(rbf2(:)-rbf3(:)) < 1e-15 )结果:Elapsed time is 2.108313 seconds. # PDIST2Elapsed time is 1.975865 seconds. # BSXFUNElapsed time is 2.706201 seconds. # REPMATz - matrix of doubles, size Nx2;x - matrix of doubles, size Nx2;sup = x(i, :);phi(1, i) = {@(z) exp(-g * sum((z - sup(ones([size(z, 1) 1]),:)) .^ 2, 2))};this is a Radial Basis Function (RBF) for logistic regression. Here is the formula:I need your advice, can i optimize this formula? coz it calls millions times, and it takes a lot of time... 解决方案 It seems in your recent edits, you introduced some syntax errors, but I think I understood what you were trying to do (from the first version).Instead of using REPMAT or indexing to repeat the vector x(i,:) to match the rows of z, consider using the efficient BSXFUN function:rbf(:,i) = exp( -g .* sum(bsxfun(@minus,z,x(i,:)).^2,2) );The above obviously loops over every row of xYou can go one step further, and use the PDIST2 to compute the euclidean distance between every pair of rows in z and x:%# some random dataX = rand(10,2);Z = rand(10,2);g = 0.5;%# one-line solutionrbf = exp(-g .* pdist2(Z,X,'euclidean').^2);Now every value in the matrix: rbf(i,j) corresponds to the function value between z(i,:) and x(j,:)EDIT:I timed the different methods, here is the code I used:%# some random dataN = 5000;X = rand(N,2);Z = rand(N,2);g = 0.5;%# PDIST2ticrbf1 = exp(-g .* pdist2(Z,X,'euclidean').^2);toc%# BSXFUN+loopticrbf2 = zeros(N,N);for j=1:N rbf2(:,j) = exp( -g .* sum(bsxfun(@minus,Z,X(j,:)).^2,2) );endtoc%# REPMAT+loopticrbf3 = zeros(N,N);for j=1:N rbf3(:,j) = exp( -g .* sum((Z-repmat(X(j,:),[N 1])).^2,2) );endtoc%# check if results are equalall( abs(rbf1(:)-rbf2(:)) < 1e-15 )all( abs(rbf2(:)-rbf3(:)) < 1e-15 )The results:Elapsed time is 2.108313 seconds. # PDIST2Elapsed time is 1.975865 seconds. # BSXFUNElapsed time is 2.706201 seconds. # REPMAT 这篇关于Matlab公式优化:径向基函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 03:58