Sum of a two dimensional array

后端 未结 1 531
忘了有多久
忘了有多久 2021-01-19 09:27

I have this 2D array L(i,j). How can I sum all the elements depending of i and make the result as a function of j

I did :

          


        
相关标签:
1条回答
  • 2021-01-19 09:51

    Almost... you don't use i (and you don't need to), and you are missing one bracket:

    do j=1,10
      T(j) = Sum( L(:,j) )
    enddo ! j
    

    You could also use the dimension parameter in sum to do this operation in one line:

    T = sum( L, dim=1 )
    

    However, I find that very difficult to read and would stick with the loop - it shouldn't make a difference in terms of performance.

    0 讨论(0)
提交回复
热议问题