Vectorizing bsxfun

조회 수: 2 (최근 30일)
Brendan
Brendan 2011년 8월 16일
Hi,
I have two matrices (which are really lists of vectors) and would like a matrix of the pair-wise squared distances between all of them. The following code does what I want, but I'm curious if there's any way to vectorize this.
Thank you
rX=rand(nTrain,numDim);
rXClass=rand(nClass,numDim);
dists=zeros(nTrain,nClass);
for ii=1:nTrain
thisX=rX(ii,:);
dists(ii,:)=sum(bsxfun(@minus,thisX,rXClass).^2,2)/D;
end

답변 (3개)

the cyclist
the cyclist 2011년 8월 16일
rX2 = permute(rX,[1 3 2]);
rXClass2 = permute(rXClass,[3 1 2]);
dists = sum(bsxfun(@minus,rX2,rXClass2).^2,3)/D;

Sean de Wolski
Sean de Wolski 2011년 8월 16일
dists2 = squeeze(sum(bsxfun(@minus,rX,reshape(rXClass',[1 numDim nClass])).^2,2))/D;
With all three sizes equaling 150, I have your elementary for-loop running the fastest:
nTrain = 150;
numDim = 150;
nClass = 150;
D = 1;
rX=rand(nTrain,numDim);
rXClass=rand(nClass,numDim);
t1 = 0;
t2 = 0;
t3 = 0;
for jj = 1:50
tic
dists=zeros(nTrain,nClass);
for ii=1:nTrain
thisX=rX(ii,:);
dists(ii,:)=sum(bsxfun(@minus,thisX,rXClass).^2,2)/D;
end
t1 = t1+toc;
tic
dists2 = squeeze(sum(bsxfun(@minus,rX,reshape(rXClass',[1 numDim nClass])).^2,2))/D;
t2 = t2+toc;
tic
rX2 = permute(rX,[1 3 2]);
rXClass2 = permute(rXClass,[3 1 2]);
dists3 = sum(bsxfun(@minus,rX2,rXClass2).^2,3)/D;
t3 =t3+toc;
end
isequal(dists,dists2,dists3)
[t1 t2 t3]
ans =
1
ans =
3.1505 4.0336 4.0368
  댓글 수: 4
Sean de Wolski
Sean de Wolski 2011년 8월 16일
Nope, probably not. The only hope I can think of for that one is maybe with James' mtimesx on File Exchange.
the cyclist
the cyclist 2011년 8월 17일
If you prefer the vectorized code, they can both be sped up a fair amount by pulling apart the one-liner calculation of "dists" into separate lines for the bsxfun call, the squaring, and the sum

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2011년 8월 16일
my small contribution:
[a b] = meshgrid(1:nTrain,1:nClass);
dists = reshape(sum((rX(a(:),:) - rXClass(b(:),:)).^2,2),[],nTrain)'/D

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by