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
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
14 Comments
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417107
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417107
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417110
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417110
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417139
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417139
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417159
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417159
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417164
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417164
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417165
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417165
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417173
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417173
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417203
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417203
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417257
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417257
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417259
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417259
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417260
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417260
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417262
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417262
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417263
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417263
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417266
Direct link to this comment
https://es.mathworks.com/matlabcentral/answers/318619-speed-up-matrix-subtraction-for-euclidean-distance-calculation#comment_417266
Sign in to comment.