我有一個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