Fortran95 Dynamic Array: Reducing the array size multiple times, is it possible?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alien
    New Member
    • Sep 2007
    • 61

    Fortran95 Dynamic Array: Reducing the array size multiple times, is it possible?

    Hi Guys,

    I am currently making a serial algorithm for clustering. I start off with a dynamic array using "ALLOCATABL E" and prompt user for a size then use the ALLOCATE() function to allocate the size.

    Now, problem is as this is a cluster algorithm, when I start off with say 12 data points and assuming that each 12 points are on their own cluster, after the first run, I will have 11 data points after I merge 2 of them (algorithm says to merge 2 closest data points by weighted average and you keep on doing that until you cant merge any more because remaining clusters have distance greater than the euclidean norm you chose earlier).

    Thing is I now gotta update my array I declared earlier of 12 data points with 11 so I can overwrite one of the entry but gotta get rid of the other. So after every run I gotta now replace the existing n*2 array with (n-1)*2 array. Its a 2D array to store x-coordinate and y-coordinate of data points.

    Is there anyway Fortran allows for this or any idea how to get around this?

    I looked at DEALLOCATE() but that wipes out the entire array which isn't what I need.

    Thanks for looking at this.

    Cheers.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    ALLOCATE() a new array, copy over the elements from your old array (only the needed ones) and DEALLOCATE() your old array. I only know Fortran 77 (and it's more than rusty) so I wouldn't know if there's a REALLOCATE() function available; check your manuals.

    kind regards,

    Jos

    Comment

    • Alien
      New Member
      • Sep 2007
      • 61

      #3
      Originally posted by JosAH
      ALLOCATE() a new array, copy over the elements from your old array (only the needed ones) and DEALLOCATE() your old array. I only know Fortran 77 (and it's more than rusty) so I wouldn't know if there's a REALLOCATE() function available; check your manuals.

      kind regards,

      Jos
      Actually I misunderstood Deallocate(), It simply wipes it clean but not removes the array from memory.

      So if I do this:

      [code=fortran]
      PROGRAM test
      REAL, DIMENSION(:,:), ALLOCATABLE :: input
      INTEGER :: n

      ! First run
      allocate(input( 5,5))
      n = size(input)
      deallocate(inpu t)


      ! Second run
      allocate(input( 3,2))
      n = size(input)
      WRITE(*,*) n


      END
      [/code]

      So on first run, i get 10 and on second run I get 6. So Array is still there. Initially I thought the array gets deleted and you had to created a new array and as you know Fortran, you have to create all your variables before executables which would have caused me problems had it really deleted instead of wiped clean.

      So I still gotta do my copy and paste but at least I won't have to work with 50 arrays!

      Thanks for looking at this.

      Comment

      Working...