0
votes

I am a newbie with Matlab and I have the following scenario( which is part of a larger problem).

matrix A with 4754x1024 and matrix B with 6800x1024 rows.

For every row in matrix A i need to calculate the euclidean distance in matrix B. I am using the following technique to calculate the distance but I find that this is very inefficient and very time consuming in Matlab.

for i=1:row_A
  A_data=A_test(i,:);
  for j=1:row_B
     B_data=B_train(j,:);
     X=[A_data;B_data];
     %calculate distance
     d=pdist(X,'euclidean');
     dist(j,i)=d;
  end
end

Any suggestions to optimise this because the final step involves performing this operation on 50 such sets of A and B.

Thanks and Regards,

Bhavya

1
Your current code does not use A_data or B_data, and test_data and train_data and train_class have no definition. - Hannes Ovrén
No idea whatsoever on matlab stuff, but the most common optimization in calculating euclidean distance is not to do it. Instead, computer the squared distance, and compare squared distances (square any raw values you want to compare against, too). This saves one reciprocal square root per distance. - Damon
@kigurai thank you for pointing this out to me, I have made the necessary corrections. At my end I have the matrix definitions. A_test and B_train are 2 very large matrices with the dimensions as mentioned in the problem statement. - bhavs
@Damon it is a requirement for me to use euclidean, though I can use the method you have mentioned, I would like to know if there is a way for me to aviod loops - bhavs

1 Answers

1
votes

I'm not sure what your code is actually doing.

Assuming your data has the following properties

assert(size(A,2) == size(B,2))

Try

d = zeros(size(A,1), size(B,1));
for i = 1:size(A,1)
    d(i,:) = sqrt(sum(bsxfun(@minus, B, A(i,:)).^2, 2));
end

Or possibly better organised by columns (See "Store and Access Data in Columns" in http://www.mathworks.co.uk/company/newsletters/news_notes/june07/patterns.html):

At = A.'; Bt = B.';
d = zeros(size(At,2), size(Bt,2));
for i = 1:size(At,2)
    d(i,:) = sqrt(sum(bsxfun(@minus, Bt, At(:,i)).^2, 1));
end