Matlab Code Optimization in calulating euclidean distance between vectors

1 次查看(过去 30 天)
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 个)

Alan Weiss
Alan Weiss 2013-2-12
If you use profile to check which part of your code is taking the most time, I guess you will find that the load operations are your bottleneck. If this guess turns out to be true, then you need to find a way to load just one data file. For example, you could concatenate all your data files into one big matrix, with possibly different lengths that you store in another file. Then just load once, and process the data in chunks.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 个评论
.. Vamshi
.. Vamshi 2013-2-13
编辑:.. Vamshi 2013-2-13
I can load them at once in the beginning so no problem there. The problem is I am calculating euclidean distance of each vector with one another which executes 135x135 (if N=135). I want to cut the time in computation here. So help me in this regard.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by