Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of a two dimensional array

Tags:

sum

fortran

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 :

 do j=1,10
  do i =1,30
   T(j) = Sum( L(:,j)
  end do 
 end do

Is that ok?

like image 953
C OBMOM Avatar asked Nov 07 '16 19:11

C OBMOM


1 Answers

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.

like image 191
Alexander Vogt Avatar answered Nov 03 '22 10:11

Alexander Vogt