another method to for loop

1 次查看(过去 30 天)
muhammad ismat
muhammad ismat 2015-5-27
how i can change for loop to code that is faster because i have a large dataset
for i=1:n,
for j=1:n,
if ind(i) == ind(j)
s(i,j)=abs(z(i,ind(i))-z(j,ind(j))) / (z(i,ind(i))+z(j,ind(j)))
else
s(i,j)=abs(z(i,ind(j))-z(j,ind(i))) / (z(i,ind(j))+z(j,ind(i)))
end
end
end
where n is the no of rows ind is the cluster number z is the distance from point to cluster no s is the similarity between any two point

回答(1 个)

Roger Stafford
Roger Stafford 2015-5-27
There is one obvious way to speed things up. Your test for ind(i)==ind(j) is totally unnecessary and can be eliminated, because the expressions
abs(z(i,ind(i))-z(j,ind(j)))/(z(i,ind(i))+z(j,ind(j)))
and
abs(z(i,ind(j))-z(j,ind(i)))/(z(i,ind(j))+z(j,ind(i)))
are identically equal when ind(i) equals ind(j). Just write
for i=1:n
for j=1:n
s(i,j) = abs(z(i,ind(j))-z(j,ind(i)))/(z(i,ind(j))+z(j,ind(i)));
end
end
Even more important is that you should make sure the 's' matrix has been preallocated the proper amount of memory space before entering these for-loops. That make an enormous difference in speed. Doing an initial 'zeros' call with the appropriate dimensions would do the job.
I see a third way to possibly get more speed. You have symmetry in the placement of values in the 's' matrix, so you can cut the number of computations with 'z' by almost half.
for i = 1:n
for j = 1:i % <-- Note the 1:i instead of 1:n
s(i,j) = abs(z(i,ind(j))-z(j,ind(i)))/(z(i,ind(j))+z(j,ind(i)));
s(j,i) = s(i,j);
end
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by