another method to for loop

1 view (last 30 days)
muhammad ismat
muhammad ismat on 27 May 2015
Answered: Roger Stafford on 27 May 2015
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

Answers (1)

Roger Stafford
Roger Stafford on 27 May 2015
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!