Larusson
Larusson

Reputation: 267

How do I index every Nth element of an array in Fortran?

I am struggling to index every nth element of an array in Fortran.

I have an array of 24 hours x 365 days x 91 steps of latitude, which I have declared as

 integer, dimension(1:24, 1:365, 1:91) :: my_array

I now would like to pick only every nth (lets say 10th) degree of latitude do reduce the resolution and write them no a second array

integer, dimension(1:24, 1:365, 1:10) :: my_new_array

I have no problem writing any 10 consecutive degrees of latitude to the new array e.g.

my_new_array = my_array(:,:,50:60)

but cant figure out how to do it with every nth element. In R or Matlab I would simply write a sequence from 1 to 91 by steps of 10, however that doesn't work for Fortran.

Upvotes: 1

Views: 1718

Answers (1)

Jack
Jack

Reputation: 6158

You can add a third element to the array slice:

my_new_array = my_array(:,:,1:91:10)

See section 6.5.3 Array elements and array sections in the Fortran Standard.

Since this is latitude, you might want to consider going from 0 to 90:

integer, dimension(1:24, 1:365, 0:90) :: my_array

And if you are doing the southern hemisphere, too:

integer, dimension(1:24, 1:365, -90:90) :: my_array

Upvotes: 2

Related Questions