In the simplest case, I want to create a 2*2 markov transition matrix as an input to a program, something like
A = [0:977 , 0:023
0:074 , 0:926 ]
That would be trivial in matlab, R or python, but it seems not that simple in low level language like fortran. I guess I cannot write something like
REAL,DIMENSION(2,2) :: P
P(1,1)= 0.977, P(1,2) = 0.023, P(2,1)= 0.074, P(2.2)=0.926
And I do some search, it's likely I will have to use read(*,*)
, but I'm not sure. So tell me how to do, thank you.
—————————— Supplementary——————————
Thank you @Fl.pf. for helpful guidence
A more specific example, I adjust a existing module to add two new parameters, both are in form of matrix
module parameters
implicit none
REAL, PARAMETER :: b = 0.99, d = 0.025, a = 0.36
REAL, PARAMETER :: klb = 0.01, inc = 0.025, kub = 45.0
INTEGER, PARAMETER :: length_grid_k = (kub-klb)/inc + 1
INTEGER, PARAMETER :: length_z = 2
REAL , PARAMETER :: toler = 1.e-4 ! Numerical tolerance
!REAL,DIMENSION(length_z,length_z) :: P ! Trasition matrix of technology process
REAL :: P(2,2)
P(1,:) = (/0.977, 0.023/)
P(2,:) = (/0.074, 0.926/)
!REAL,DIMENSION(2) :: y ! technology
REAL :: y(2,1)
y = (/1.25, 0.2/)
end module
As you can see, the P
and y
are matrice I add later on and where the error take place. The gfortran complier throw me error message like:
Unexpected assignment statement in module
So if I don't take the answer from @Fl.pf. for wrong, then I can only assign values to the matrix by 1) allocate space for the matrix by real, allocatable, dimension(:,:); 2)
assign the value allocate(transition(n_tauchen,n_tauchen))
. Am I right? But some code suggest alternatives, like the following snippet of code
program matrices
implicit none
integer :: i, j
real*8 :: a(4), b(4)
real*8 :: x(2, 4), y(4, 2), z(2, 2)
! initialize vectors and matrices
a = (/(dble(5-i), i=1, 4)/)
b = a+4d0
x(1, :) = (/1d0, 2d0, 3d0, 4d0/)
x(2, :) = (/5d0, 6d0, 7d0, 8d0/)
y = transpose(x)
z = matmul(x, y)
It seems they assign values to matrix x
directly. I'm a little bit confusing here , what's the difference?