从Fortran数组中删除特定元素 [英] Delete a specific element from a Fortran array

查看:502
本文介绍了从Fortran数组中删除特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran中是否有一个函数可以删除数组中的特定元素,以便删除后的数组将其长度缩小已删除元素的数量?

Is there a function in Fortran that deletes a specific element in an array, such that the array upon deletion shrinks its length by the number of elements deleted?

背景: 我目前正在一个项目中,该项目包含多组人口,并对每个人都有相应的描述(即年龄,死亡年龄等).

Background: I'm currently working on a project which contain sets of populations with corresponding descriptions to the individuals (i.e, age, death-age, and so on).

我使用的一种方法是循环遍历数组,找到我需要的元素,将其放置在另一个数组中,并取消分配前一个数组,然后在下一个时间步骤之前,将该数组移回该数组,然后再进行操作.子例程,以再次找到不需要的元素.

A method I use is to loop through the array, find which elements I need, place it in another array, and deallocate the previous array and before the next time step, this array is moved back to the array before going through the subroutines to find once again the elements not needed.

推荐答案

您可以使用PACK内部函数和内部分配来创建一个数组值,该值由另一个数组中的选定元素组成.假定array是可分配的,并且要删除的元素由逻辑掩码logical_mask提名,该逻辑掩码的大小与array的原始值相同:

You can use the PACK intrinsic function and intrinsic assignment to create an array value that is comprised of selected elements from another array. Assuming array is allocatable, and the elements to be removed are nominated by a logical mask logical_mask that is the same size as the original value of array:

array = PACK(array, .NOT. logical_mask)

由其索引指定的单个元素的简洁语法为:

Succinct syntax for a single element nominated by its index is:

array = [array(:index-1), array(index+1:)]

取决于您的Fortran处理器,以上语句可能会导致编译器创建可能会影响性能的临时变量.如果这有问题,那么您将需要使用您描述的子例程方法.

Depending on your Fortran processor, the above statements may result in the compiler creating temporaries that may impact performance. If this is problematic then you will need to use the subroutine approach that you describe.

这篇关于从Fortran数组中删除特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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