% each row in A is an example.A=[1210;1420;1615];C=bsxfun(@minus,A,mean(A))

更厉害的的是用bsxfun计算距离矩阵

d=300;k=400;n=500;A=rand(d,k);B=rand(d,n);dist=zeros(k,n);%计算任意 A(;,ii)到B(:,jj) 的欧式距离% naive implementaionticforii=1:kforjj=1:ndist(ii,jj)=sqrt(sum((A(:,ii)-B(:,jj)).^2));endendtoc%Elapsed time is 0.368322 seconds% more effective manner, using bsxfunticdist=sqrt(-bsxfun(@minus,bsxfun(@minus,2*A'*B,sum(A.^2,1)'),sum(B.^2,1)));toc%Elapsed time is 0.006528 seconds% if A and B row is an example, i.e. A=rand(k,d);B=rand(n,d);dist=sqrt(-bsxfun(@minus,bsxfun(@minus,2*A*B',sum(A.^2,2)),sum(B.^2,2)'));

c=mat2cell(1:1e5,1,repmat(1,1,1e5));tic,d=cellfun('isempty',c);toc%Elapsed time is 0.001119 secondstic,d=cellfun(@isempty,c);toc%Elapsed time is 0.062364 secondstic,d=cellfun(@(x)isempty(x),c);toc%Elapsed time is 0.389135 secondsticfori=1:length(c)d(i)=isempty(c{i});endtoc%Elapsed time is 0.057377 secondsA=cell(100,1);B=cell(100,1);D=cell(100,1);fori=1:100A{i}=rand(500,500);B{i}=rand(500,500);endtic;C=cellfun(@plus,A,B,'un',false);toc%Elapsed time is 0.087974 secondsticfori=1:100D{i}=A{i}+B{i};endtoc%Elapsed time is 0.079868 seconds