在Fortran中,如何从一个数组中删除第N个元素? [英] In Fortran, how do I remove Nth element from an array?
问题描述
例如我有阵列(/ 1,3,4,5,7,9,11 /),我怎么删除其3元?我找不到它确实是一个阵列的功能,也没有我发现一个循环一个优雅的解决方案,因为我不知道该怎么追加到一个数组(这意味着,添加元素的previous定义的元素旁边。 )
Eg I have array (/1,3,4,5,7,9,11/), how do I remove its 3rd element? I couldn't find an array function which does that, nor I find a loop an elegant solution, since I don't know how to append to an array (this means, add an element next to the previous defined element.)
我想从一个数组中删除所有偶数元素......我知道,只有一个。
I want to remove all even elements from an array... I know there is only one.
我可以找到使用它的索引 MINLOC ,但我不知道如何从数组中删除一个元素。
I can find its index using MINLOC, but I don't know how to remove an element from array.
推荐答案
让
a = (/1,3,4,5,7,9,11/)
然后
pack(a,mod(a,2)/=0)
将返回的奇数元素
。这是不太一样去掉了第三个元素,但你的问题表明,去除偶数元素(s)是你真正想要做的事情。
will return the odd elements of a
. This isn't quite the same as removing the 3rd element, but your question suggests that removing the even element(s) is really what you want to do.
如果您声明
integer, dimension(:), allocatable :: oddones
然后
oddones = pack(a,mod(a,2)/=0)
将离开 oddones
包含的奇数元素
。你需要一个向上最新的编译器使用这种自动分配。
will leave oddones
containing the odd elements of a
. You'll need an up-to-date compiler to use this automatic allocation.
请注意,在Fortran语言,如任何理智的语言中,数组是固定大小的,所以移除元素是不是真的支持。但是,如果 A
本身是可分配
那么你可以使用 A
上的 LHS 的前任pression的。让我们把它留到哲学家与否 A
保持此操作下是相同的。
Note that in Fortran, as in any sane language, arrays are of fixed size so removing an element isn't really supported. However, if a
itself were allocatable
then you could use a
on the lhs of the expression. Let's leave it to the philosophers whether or not a
remains the same under this operation.
这篇关于在Fortran中,如何从一个数组中删除第N个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!