如何写一个巨大的矩阵逐行存档(fortran 90) [英] how to write a huge matrix to file row by row (fortran 90)
问题描述
我想将一个包含大量数据的矩阵逐行写入一个文件。例如,我有一个100 * 100的矩阵,我想在表单100 * 100的文件中。但是,它不起作用。以下是我的代码和一些说明。 N和M是大约数百个整数。 RECL预计的长度我设置文件,但在这里看起来这个命令不起作用。当N设为99
且M设为200时,输出为198行.Vec是双精度复数矩阵。我怎样才能输出维克原始格式N * M的值?我的编译命令是ifort -o out test.f90。
open(unit = 2,file ='graph1.txt ',ACTION =write,STATUS =replace,RECL = 40 * M + 10)
do i = 1,N
do j = 1,M
write(2, '(F)',advance ='no')real(Vec(i,j))
end do
write(2,*)''
end do
在@george建议之后,我编写了这样的程序:
程序测试
隐式无
整型i,j
open(unit = 2,file ='graph1.txt ',ACTION =write,STATUS =replace)
do i = 1,500
write(2,'(1600F14.7)')(0.00001,j = 1,499)
end (2)
结束
使用此代码,问题解决了!也许我上次没有正确编译。
扩展我的评论,你也应该使用隐式循环..这:
open(单元= 2,文件='graph1.txt',ACTION =写入,STATUS =替换)
do i = 1,N
write(2,'(1000F14.7)')(real(Vec(i,j)),j = 1,M)
end做
或者对于足够现代的编译器(我不确定这是多么的新鲜......)
(2,'(*(F14.7))')(real(Vec(i,j)),j = 1,...,b M)
注意如前所述,括号(F14。 7)
是2008标准中 *
unlimited-format-item所必需的。
<你可以这样做:
write(2,'(*(F14 .7))')real(Vec(i,:M))
I want to write a matrix with a lot of data to a file row by row. For example, I have a matrix 100*100 and I want to have it in form 100*100 in the file. However, it doesn't work.Following is my code and some description. N and M are integers around some hundreds. RECL is expected length I set the file but here it seems this command does not work. The output is with 198 lines when N is set 99 and M is set 200. Vec is a double precision complex matrix. How could I output the values of Vec keeping its original format N*M? My compile command is "ifort -o out test.f90".
open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace",RECL=40*M+10)
do i=1,N
do j=1,M
write(2, '(F)', advance='no') real(Vec(i,j))
end do
write(2, *) ''
end do
Following @george advice, I coded a program like this:
program test
implicit none
integer i,j
open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace")
do i=1,500
write(2, '(1600F14.7)')( 0.00001 ,j=1,499)
end do
close(2)
end
With this code, problem solved! Maybe I didn't compile correctly last time.
expanding on my comment, you should also use an implicit loop..try this:
open(unit=2, file='graph1.txt', ACTION="write", STATUS="replace")
do i=1,N
write(2, '(1000F14.7)')( real(Vec(i,j)) ,j=1,M)
end do
or for sufficiently modern compilers (I'm not sure how new.. )
write(2, '(*(F14.7))')( real(Vec(i,j)) ,j=1,M)
Note as has been pointed out, the parenthesis around (F14.7)
are required for the *
unlimited-format-item in the 2008 standard.
may as well pull in the other comments, you can also do this:
write(2, '(*(F14.7))')real(Vec(i,:M))
这篇关于如何写一个巨大的矩阵逐行存档(fortran 90)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!