Home > Back-end >  Asking proper do loop fortran
Asking proper do loop fortran

Time:12-08

I write a code, for measuring distance k means.

So, r is array for my data points. I want to reduce every coordinate of data point with every centroid. So, flow are like, J only change if y=k . Meanwhile i should be keep on going (i number should keep updated 1). distance is 2d array, and Z is the column indeks. Distance is empty 2d array that the value will be filled by result of reduction, that is why the value of i should be updated for every calculation. N is number of data points, and k is number of cluster. z, k , and n is different value.

the problem my with my current code,

do i=1,z
   do j=1,n
      y=1
     do while 
        y<=k 
        distance(1,i)=abs( r(1,j) - centroid (1,y) )
        distance(2,i)=abs( r(2,j) - centroid (2,y) )
        y=y 1
     end do
   end do
end do

The problem with this is, the i value is not keep updated. I need i value keep increase, 1

CodePudding user response:

This is something of a guess, as I'm still confused by your question, but is this what you want?

i = 0
do j=1,n
  do y=1,k
    i = i 1
    distance(:,i) = abs(r(:,j)-centroid(:,y))
  enddo
enddo

So looping j over the r array, y over the centroid array (but only up to k), and incrementing i for each combination of the two?

This assumes that the distance array is large enough that i doesn't overrun it.

  • Related