索引Fortran数组的多个非相邻元素 [英] Index multiple non-adjacent elements of a Fortran array

查看:89
本文介绍了索引Fortran数组的多个非相邻元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran中有没有一种方法可以在不使用循环的情况下访问数组的许多元素?

Is there a way in Fortran to access many elements of an array without using a loop?

例如给定的100个元素的数组

For example given array of 100 elements

real(100) :: a

我可以做这样的事情来访问不遵循常规步骤的元素1,4,7,54,81吗?

can I do something like this to access elements 1,4,7,54,81 that do not follow a regular step?

a(1,4,7,54,81)= 3.21423

推荐答案

如前所述,数组可用作数组的索引.这就是所谓的向量下标.

As noted before, an array may be used as the indexes of an array. This is a so-called vector subscript.

A([1,4,7,54,81]) = 3.21423

设置赋予该值的元素. (这与先前的答案相同,但使用的是Fortran 2003 +/现代数组构造函数表示法.)

sets the elements given to that value. (This is the same as the earlier answer but using the Fortran 2003+/modern array constructor notation.)

该数组可以是任何等级1的数组,例如变量或表达式:

The array can be any rank-1 array, such as a variable or expression:

integer :: idx(5)=[1,4,7,54,81]
A(idx) = 3.21423
A(idx-1+1) = 3.21423

当然,矢量下标还可以用于其他设置,例如引用:

Of course, vector subscripts are of use in other settings, such as referencing:

print *, A(idx)
call sub(A(idx))
A(idx) = A(idx+1) + 5

但是,带有向量下标的数组节受到各种限制,例如:

However, array sections with vector subscripts are subject to various restrictions, such as:

  1. 并非总是它们是过程的参数;
  2. 指针可能无法指向它们;
  3. 并非所有此类部分都可以分配给

在第三种情况下,如果同一索引在下标中多次出现,则无法定义它.所以

In the third case, if the same index appears more than once in the subscript we can't define it. So

print *, A([1,5,1])

允许

,但是

A([1,5,1]) = 3.

不是.

这篇关于索引Fortran数组的多个非相邻元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆