MATLAB Answers

Speed up Matrix Subtraction for Euclidean Distance calculation

7 views (last 30 days)
TYS
TYS on 30 Dec 2016
Commented: Walter Roberson on 31 Dec 2016
I have 2 matrices, both of them are feature matrices, M1 is 9216x26310, M2 is 9216x34000. each column represents 9216x1 represents one feature for a particular image, in this case 26310 images for the M1, 34000 for M2. I want to speed up the time for it to calculate euclidean distance, my current code takes average 1.3 secs or 1.4 secs per calculation.
tic;
for idx = 1:Number_of_Test_Images
for TrnIdx = 1 : Number_of_Train_Images
E_distance(TrnIdx) = sqrt(sum(abs((ftest(:,idx)-ftrain(:,TrnIdx)).^2)));
end
%
%[smallest_value, Idx_smallest] = min(E_distance);
%
Esimate_Test_labels(idx,1) = TrnLabels(Idx_smallest,1);
toc;
end
even if I replace the nested for loop with this
[smallest_value,Idx_smallest] = min(sum(abs(bsxfun(@minus,ftest(:,idx),ftrain))));
it still takes roughly the same time, although this one doesn't calculate sqrt.
Is there other way to speed this up to about 0.2 secs per calculation?
I have a i7 6700k CPU sadly no Nvidia GPU, owning AMD GPU. Else I can use gpuarray to speed things up.
Any ideas guys?
Both matrices are type double, I've tried to use sparse double on the my original code, my computer hangs every time after it reaches maximum memory usage, had to restart. and Sparse double matrices actually takes longer to compute.
Update! I've tried convert both matrices into single precision, and it runs faster, average of 0.63 secs per calculation by using the formula below, which runs the fastest compare to others.
I still need it to run faster, preferably under 0.2 secs if that's possible.
for TrnIdx = 1 : Number_of_Train_Images
E_distance(TrnIdx) = norm(ftest(:,idx)-ftrain(:,TrnIdx));
end
% runs averagely of 0.63secs which is the best at the moment, after converting both matrices from double to single, double runs at 1.05secs

  14 Comments

Ahmet Cecen
Ahmet Cecen on 31 Dec 2016
This is correct. However, to achieve what he actually wants to do, he doesn't actually need to do that many subtractions. His original code is asking the wrong question, and finding the answer inefficiently on top of that.
The right question here is the nearest neighbor problem. Optimizations of queries of this nature (search) is a very well studied area, with many exact and approximate solutions.
I believe the MATLAB implementation is taking advantage of some tree structure to do a fast exact search, reducing the number of necessary comparisons by structuring the searched data in a particular way.
A very optimized approximate algorithm is FLANN which has MATLAB bindings. This should further reduce the execution time of the entire task to around 3-5 minutes, with a minor fraction of inaccurate results (corresponding to the 5th nearest neighbor instead of 1st, for example).
TYS
TYS on 31 Dec 2016
I do have multi cores, unless you're talking about parfor loop? Never mind that, I've got the answer already.
NS = createns(Ftrain');
Idx = knnsearch(NS,Ftest');
Which gives all the 26310 smallest indexes that I wanted. and It took only 2280 secs, which is 38 minutes.
Tested that code above with my code, gives the same result.
Walter Roberson
Walter Roberson on 31 Dec 2016
My testing with parfor and norm indicated it was about 7 times slower than a straight forward norm loop.

Sign in to comment.

Accepted Answer

Ahmet Cecen
Ahmet Cecen on 30 Dec 2016
If you have the stats & ML toolbox:
NS = createns(M1'); % Create Search Object for The Train Set
Idx = knnsearch(NS,M2'); % Search Nearest Element of M1 for Each Element in M2
This should be lightning fast compared to your original code, probably would take around 20-30 minutes for the entire task. If you run out of memory, chunk the test matrix into smaller groups, and provide that as input, instead of looping over them one-by-one with i.

  3 Comments

TYS
TYS on 31 Dec 2016
the Idx matrix I get from your code seems wrong, When I pass it a single feature ftest(:,1), it gives a Idx matrix of 34000x1 with all 1 filled in the matrix.
Ahmet Cecen
Ahmet Cecen on 31 Dec 2016
Are you sure you are using the Tranpose matrices? Each row should be an image, each column a dimension.
This works for me without any error.
Here:
A = rand(9216,26310);
>> B = rand(9216,10);
>> tic;NS = createns(A');toc;
Elapsed time is 1.410517 seconds.
>> tic; Idx = knnsearch(NS,B'); toc;
Elapsed time is 1.992841 seconds.
>> Idx
Idx =
15172
22166
1461
17563
6626
16701
12601
10224
16658
15376
Your code results:
tic;
for idx = 1:10
for TrnIdx = 1 : 26310
E_distance(TrnIdx) = sqrt(sum(abs((B(:,idx)-A(:,TrnIdx)).^2)));
end
[smallest_value, Idx_smallest] = min(E_distance);
YourMethodIDX(idx) = Idx_smallest;
end
toc;
Elapsed time is 11.048696 seconds.
YourMethodIDX'
ans =
15172
22166
1461
17563
6626
16701
12601
10224
16658
15376
Note that while the speed improvement here is marginal, it will be Orders of Magnitude faster if you input your entire matrix.
TYS
TYS on 31 Dec 2016
Thanks so much, now from from 4h30min+ to 38 minutes!!! Thanks so so much.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 30 Dec 2016
Edited: Matt J on 30 Dec 2016
You could try DNORM2( Download ), as applied to
E_distance = DNorm2( bsxfun(@minus,ftest(:,idx),ftrain) ,1) ; %Equation (*)
You could also try parallelizing the outer loop with parfor and maybe even the computation of E_distance as well (break ftrain into smaller parallel chunks and perform pieces of Equation (*) above on different workers).
Finally, if ftest and ftrain are type double, you might try moving to type single.

  5 Comments

Show 2 older comments
TYS
TYS on 31 Dec 2016
I'm running into tons of problem on installing that compiler. Matlab is really crappy at this huh.
Matt J
Matt J on 31 Dec 2016
No, it's usually not a big problem. Tech support might be able to help you with the installation.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by