0

My IDE is CodeBlocks with MinGW 9.2.0.

I need a help with writing matrix row by row in textual file.

This is my simple code:

program matrix
 
 integer :: i, j 
 integer, dimension(2,2) :: a_mat
 
 forall( i = 1 : 2, j = 1, 2 )
    
   a_mat(i,j) = i + j

 end forall 
 
 open( unit = 15, file = 'matrix_utput.txt', action = 'write' )
  
   write(15,'(*(i2.2,1x))') ( ( a_mat(i,j), j = 1, 2 ), i = 1, 2 )
 
 close( unit = 15 )

end program matrix

In my .txt file i got this: 02 03 03 04 How to change format to get this:

02 03

03 04

1 Answers1

1

The normal way is to loop in a do loop line by line as shown at Write matrix with Fortran

But one can also do it in an implied loop using format reversion.

 write(15,'(2(i2.2,1x))') ( ( a_mat(i,j), j = 1, 2 ), i = 1, 2 )

Basically, the number in front of the parenthesis must be the row length (number of columns). If you put the * there, it will consume the whole array. This way, it will open a new record each time the format found all its items.