Reading an Rank 2 array in FORTRAN 95 -
i want read array text file looks this
1 88 90 94 102 111 122 134 2 75 77 80 86 94 103 113 3 80 83 85 94 100 111 121 4 68 71 76 85 96 110 125 5 77 84 91 98 105 112 119 6 81 85 90 96 102 109 120
(above data 6 8 matrix, evenly spaced, might not show correctly
my code
program cool implicit none integer:: ok(6,8),k,i open(unit = 1, file = "a.txt") read(1,*) ok write(*,*) ok print*,ok_1(4,3) end program cool
the issue having ok(4,3) not correspond 71 ( 4th row down ,3rd column)
i have tried read file such read(unit =1,10) ok 10 format ( 8(i4))
! have tried variants of this
what ever way try, calling ok(i,j) not correspond value need
i need find row averages , column averages out column 1 included. simple loops long can correct values, can not
edit:: have tried hard code array in program ok = reshape((/1,88,90,94,102,111,122,134,& 2,75,77,80,86 ,94 ,103,113,& 3,80,83,85,94 ,100,111,121,& 4,68,71,76,85 ,96 ,110,125,& 5,77,84,91,98 ,105,112,119,& 6,81,85,90,96 ,102,109,120/),(/6,8/)) not seem make correspond aswell ok(4,4) returns 100 not 76
fortran stores arrays in memory in column-major order, element ok(1,1)
followed element ok(2,1), ok(3,1), ...
, on. convenient statement
read(1,*) ok
reads numbers array in order. using reshape
on same list of elements results in same array.
perhaps thought array stored in row-major order, ok(1,1), ok(1,2), ok(1,3), ...
?
you either read file line line, like
do ix = 1, 6 read(1,*) ok(ix,:) end
or read file in 1 gulp 8*6
temporary array , transpose it. or re-order elements in file.
Comments
Post a Comment