如何在Fortran中模仿python对数组的负索引?

问题描述:

在python中,可以使用负数组索引从头开始访问数组。如果您使用例如数组具有圆形边界条件。

In python, negative array indexing can be used to access the array starting from the end. This is particularly useful if you use e.g. arrays with circular boundary conditions.

我们可以在不使用IF子句的情况下在Fortran中模仿吗?

Can we mimic this in Fortran, without using IF clauses?

我该做什么例如,要在Fortran中获得,就是通过发出

What I want to obtain in Fortran, for example, is that by issuing

myarray(-1)

我得到数组的最后一个元素(并且与所有其他元素类似)。

I get the last element of the array (and similar with all other elements).

您可以使用以下命令获取最后一个元素:

You can get the last element using:

myarray(size(myarray))

或最后一个元素之前的元素:

or the element before the last:

myarray(size(myarray) - 1)

请注意,这仅适用于1索引数组。如果不确定,可以使用 ubound

Note that this will only work with a 1-indexed array. If you are not sure, you can use ubound.