0

I would like to merge into one. For that, I think about a function that returns a vector of two components (sum/count and count) and then direct indexing of function

My first function is this

INTEGER FUNCTION poor(x,m,n)
REAL, DIMENSION (m,n) :: x
INTEGER m, n, count, i, j
REAL cell_mean   ! This is a function that returns a real number
count = 0
DO i = 1, m
  DO j = 1, n
    IF (x(i,j) < cell_mean(x,m,n,i,j)) THEN
       count = count + 1
    ELSE
      count = count
    END IF
  END DO
END DO
poor = count

END FUNCTION

My second function is this:

REAL FUNCTION middle_value(x,m,n)
REAL, DIMENSION (m,n) :: x
INTEGER m, n, i, j count
REAL cell_mean, sum   

sum = 0
count = 0
DO i = 1, m
  DO j = 1, n
    IF (x(i,j) < cell_mean(x,m,n,i,j)) THEN
       sum = sum + x(i,j)
       count = count + 1
    ELSE
      sum = sum
      count = count
    END IF
  END DO
END DO
IF (count == 0) THEN
   middle_value = 0
ELSE
   middle_value = sum / count
END IF
END FUNCTION
  • Welcome, please take the [tour] and read [ask]. We need more information from you. The complete relevant code and the complete error message. See also [mcve]. – Vladimir F Героям слава Nov 09 '20 at 20:06
  • Please check the link and tell us if I understood your question correctly. It should contain answers to your problem. In short: what you are trying to do is not possible. – Vladimir F Героям слава Nov 09 '20 at 20:08
  • OK. Yes, you understand my question. There is a difference between the ohter question. In my case, it's not an string, it's a vector and I would like to refer each component of a vector separately. I have two function very similar that if that was possible I could merge into one. Thank you – Cristina Domínguez Nov 12 '20 at 10:41
  • It really is the same, the situation for the string and for an array is completely the same. In fact, the accepted answer shows an array, not a string! See https://stackoverflow.com/questions/37996259/fortran-indexing-expressions-that-return-an-array?noredirect=1&lq=1 and https://stackoverflow.com/questions/61265245/using-return-value-of-shape-in-fortran?noredirect=1&lq=1 – Vladimir F Героям слава Nov 12 '20 at 11:01
  • OK. Thanks. I think I can´t merge both function into one. I have edited the question to make it clearer – Cristina Domínguez Nov 12 '20 at 11:11
  • Be aware that the conclusion is that you can NOT direct-index a function result. Also, you would be calling the function two times and throw away half of the result each time. That is wasteful. You could store the result in a temporary array and index that temporary array, the techniques are in the linked question and answers. – Vladimir F Героям слава Nov 12 '20 at 12:05
  • OK, I'll see how to do it. Thanks – Cristina Domínguez Nov 13 '20 at 13:16

0 Answers0