another method to for loop

1 Ansicht (letzte 30 Tage)
muhammad ismat
muhammad ismat am 27 Mai 2015
Beantwortet: Roger Stafford am 27 Mai 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
  1 Kommentar
per isakson
per isakson am 27 Mai 2015
Bearbeitet: per isakson am 27 Mai 2015
What's ind and z?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Roger Stafford
Roger Stafford am 27 Mai 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by