如何将一个巨大的矩阵逐行写入文件(fortran 90) [英] how to write a huge matrix to file row by row (fortran 90)
问题描述
我想将包含大量数据的矩阵逐行写入文件.例如,我有一个 100*100 的矩阵,我想在文件中以 100*100 的形式拥有它.但是,它不起作用.以下是我的代码和一些描述.N 和 M 是大约数百个整数.RECL 是我设置文件的预期长度,但这里似乎这个命令不起作用.当 N 设置为 99 时,输出为 198 行M 设置为 200. Vec 是双精度复数矩阵.如何输出 Vec 的值并保持其原始格式 N*M?我的编译命令是ifort -o out test.f90".
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
按照@george 的建议,我编写了这样的程序:
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)
请注意,如前所述,(F14.7)
周围的括号对于 2008 标准中的 *
unlimited-format-item 是必需的.
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屋!