Euclidean distance between vectors

2 次查看(过去 30 天)
.. Vamshi
.. Vamshi 2013-2-23
The following code is taking a lot of time for execution say if N=135. How can I make it faster. Is there an alternative to calculate the euclidean distance between the vectors.
FVCompare= zeros(N,N); file = 'FV';
for i=1:N
sname = strcat(file,int2str(i));
sfile = strcat('FeatureVectors\',sname);
srcfile = strcat(sfile,'.mat');
for count=1:N
dname = strcat(file,int2str(count));
dfile = strcat('FeatureVectors\',dname);
destfile = strcat(dfile,'.mat');
Vec1 = load (srcfile); %loads FeatureVector-1
Vec2 = load (destfile); %loads FeatureVectors to be compared
var1 = struct2cell(Vec1);
fv1=var1{:};
var2 = struct2cell(Vec2);
fv2=var2{:};
%Finding Euclidean Distance
R = norm(fv1-fv2);
FVCompare(i,count) = R;
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2013-2-23
You can move the loading of srcfile to before the "for count" loop, as it is not going to change as count changes.
As the source file list is the same as the destination file list, comparing source 5 to destination 8 is going to give you the same result as comparing source 8 to destination 5. Therefore you only need to compare source "i" to destinations "count" when count >= i, and you can fill in the rest by symmetry.

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by