011. sum
and other array functions have an optional dim
ension argument#
topic: Arrays
integer :: v(2,3) ! 2x3 matrix
v(1,:) = [1, 2, 3]
v(2,:) = 10*v(1,:) ! [10, 20, 30]
print *, v ! elements in column-major order
print *, sum(v) ! sum all elements: 66
print *, sum(v, dim=1) ! sum each col (along row dim): 11 22 33
print *, sum(v, dim=2) ! sum each row (along col dim): 6 60
end
1 10 2 20 3 30
66
11 22 33
6 60
sum and other array functions have an optional dim(ension) argument
— FortranTip (@fortrantip) December 18, 2021
integer :: v(2,3) ! 2x3 matrix
v(1,:) = [1,2,3]
v(2,:) = 10*v(1,:) ! set to [10,20,30]
print*,sum(v) ! all elements: 66
print*,sum(v,dim=1) ! sum each row: 6 60
print*,sum(v,dim=2) ! sum each col: 11 22 33
- 1
Compiled using
GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
with no flags