我有一个file.txt的如后续等于行:如何消除在文件
1. 0. 3.21
1. 1. 2.11
1. 2. 1.554
1. 0. 3.21
1. 3. 1.111
1. 2. 1.554
正如你可以看到我有两条线中等于彼此(第一,第四和第三和第六)。我的尝试是消除是平等的,以获得类似的线路:
1. 0. 3.21
1. 1. 2.11
1. 2. 1.554
1. 3. 1.111
我Fortran程序做的尝试是:
program mean
implicit none
integer :: i,j,n,s,units
REAL*8,allocatable:: x(:),y(:),amp(:)
! open the file I want to change
OPEN(UNIT=10,FILE='oldfile.dat')
n=0
DO
READ(10,*,END=100)
n=n+1
END DO
100 continue
rewind(10)
allocate(x(n),y(n),amp(n))
s=0
! save the numbers from the file in three different vectors
do s=1, n
read(10,*) x(s), y(s),amp(s)
end do
!---------------------!
! Open the file that should contains the new data without repetition
units=107
open(unit=units,file='newfile.dat')
! THIS SHOULD WRITE ONLY NOT EQUAL ELEMENTS of THE oldfile.dat:
! scan the elements in the third column and write only the elements for which
! the if statement is true, namely: write only the elements (x,y,amp) that have
! different values in the third column.
do i=1,n
do j = i+1,n
if (amp(i) .ne. amp(j)) then !
write(units,*),x(j),y(j),amp(j)
end if
end do
end do
end program
但输出文件看起来像这样:
1.000000 1.000000 2.110000
1.000000 2.000000 1.554000
1.000000 3.000000 1.111000
1.000000 2.000000 1.554000
1.000000 2.000000 1.554000
1.000000 0.0000000E+00 3.210000
1.000000 3.000000 1.111000
1.000000 2.000000 1.554000
1.000000 0.0000000E+00 3.210000
1.000000 3.000000 1.111000
1.000000 3.000000 1.111000
1.000000 2.000000 1.554000
1.000000 2.000000 1.554000
我不明白if
条件的问题是什么,请问您能帮我一下吗?
非常感谢!
好多了。现在,您输入的文件是否真正代表了真实的输入文件?在典型的输入文件中会有多少行? – 2014-10-09 13:34:15
@HighPerformanceMark yes与三个实型列和n行(其中n = 100000(或多或少,这是输出的一般行数)的矩阵)完全相同。 – 2014-10-09 14:01:03
无论算法考虑使用字符串操作来完成整个事情(假设文本表示中“相等”行相等)。它将简化代码,速度更快,并且您的输出将被自动格式化为与输入相同。 – agentp 2014-10-09 15:56:10